!############################# Change Log ##################################
! 5.0.0
!
!###########################################################################
!  Copyright (C)  1990, 1995, 1999, 2000, 2003 - All Rights Reserved
!  Regional Atmospheric Modeling System - RAMS
!###########################################################################

subroutine initlz (name_name)

  use mem_leaf
  use mem_grid
  use mem_scratch
  use mem_basic
  use mem_micro
  use var_tables
  use mem_varinit
  use mem_cuparm
  use mem_oda
  use mem_turb, only:if_urban_canopy,akmin
  use io_params
  use micphys

  ! CATT
  use catt_start, only         : CATT                      ! intent(in)
  use mem_scalar, only         : &
       scalar_g                                            ! intent(inout)

!--(DMK-CCATT)---------------------------------------------------------
!--(DMK-original)------------------------------------------------------
!       RECYCLE_TRACERS                                     ! intent(in)
!  ! CARMA Radiation
!  use mem_globrad, only: master_read_carma_data ! Subroutine
!--(DMK-CCATT-END)-----------------------------------------------------


  ! TEB_SPM
  use teb_spm_start, only : TEB_SPM   ! intent(in)
  use mem_teb, only       : teb_g     ! intent(inout)
  use mem_teb_common, only: tebc_g    ! intent(inout)
  use teb_vars_const, only: iteb      ! intent(in)
  ! TEB_SPM: emission and chemistry
  use mem_gaspart, only   : gaspart_g ! intent(inout)
  use mem_emiss, only     : &
       ichemi,   &                    ! intent(inout)
       isource,  &                    ! intent(inout)
       ichemi_in                      ! intent(inout)

!--(DMK-CCATT)---------------------------------------------------------
  !srf - chem
  use mem_chem1, only:               &
       CHEMISTRY,                    &
       RECYCLE_TRACERS,              &
       nsrc,                         &
       nvert_src=>chem1_src_z_dim_g, &
       chem1_src_g,                  &
       bburn,                        &
       src_name,                     &
       chemistry,                    &
       ntimes_src,                   &
       diur_cycle

  use mem_chem1aq, only: CHEMISTRY_AQ

  USE mem_aer1, ONLY:  &
       aer1_g,         &
       nmodes,         &
       aerosol

  use chem_sources, only: le_sourcemaps
  use module_dry_dep, only:  dep_init ! Subroutine
  use mem_stilt, only: stilt_g, &
       iexev 

  USE chem1_list, ONLY:           &
       chem_nspecies=>nspecies,   &
       spc_chem_alloc=>spc_alloc, &
       src,                       &
       off,                       &
       spc_chem_name =>spc_name,  &
       on,                        &
       chemical_mechanism,        &
       emiss_ajust,               &
       CO

  USE aer1_list, ONLY:           &
       aer_nspecies=>nspecies,   &
       spc_aer_alloc=>spc_alloc, & 
       spc_aer_name =>aer_name,  &
       urban,                    &
       nucle,                    &
       accum

  USE mem_plume_chem1, ONLY: &
       nveg_agreg,           &
       plume_mean_g

  USE mem_plume_chem1, ONLY: &
       tropical_forest,      &
       boreal_forest,        &
       savannah,             &
       grassland,            &
       plume_g

  USE chem1_list, ONLY:           &
       transport

  USE aer1_list, ONLY:           &
       aer_bburn => bburn 

  USE grid_dims, ONLY: &
       nzpmax

  USE mem_grid, ONLY: &
       dzt,           &
       grid_g

  !srf - chem - end
!--(DMK-original)------------------------------------------------------
!  use emission_source_map, only: read_emission_sources_map ! Subroutine
!--(DMK-CCATT-END)-----------------------------------------------------


  ! Soil Moisture Init.
  use mem_soil_moisture, only : SOIL_MOIST ! INTENT(IN)

  implicit none

  character(len=*), intent(in) :: name_name
  character(len=*), parameter :: h="**(initlz)**"

  !---------------------------------------------------------------------
  !     *** This routine is the driver of all initialization packages
  !---------------------------------------------------------------------

  character(len=8) :: rest
  integer :: ifm,icm,mynum,ihm,ngr,nv,ierr

  !     Set version number for common blocks.
  iversion = 2

  !     Unit numbers for some I/O files.
  iopunt=6

  !CALL jnmbinit(level) ! Correctd by Craig.

  if (runtype(1:7) == 'INITIAL') then

     time=0.
     ngbegun(1:ngrids) = 0

     !----------------------------------------------------------------------
     !                 Initial startup
     !----------------------------------------------------------------------

     ! Read surface, topo, sst, and ndvi files for all grids. All the files
     !   were checked earlier, so they must be correct.,

     do ifm = 1,ngrids
        call top_read(ifm)
     enddo

     do ifm = 1,ngrids
        call sfc_read(ifm)
     enddo

     !     Define grid topography, transform, latitude-longitude,
     !        and map factor arrays.

     call grid_setup(2)

     ! read SST files

     do ifm = 1,ngrids
        call sst_read(1,ifm,ierr)
     enddo

     ! read NDVI files

     do ifm = 1,ngrids
        call ndvi_read(1,ifm,ierr)
     enddo

     ! Initialize snowcover arrays

     do ifm = 1,ngrids
        call snowinit(nnxp(ifm),nnyp(ifm)  &
             ,leaf_g(ifm)%snow_mass(1,1),leaf_g(ifm)%snow_depth(1,1))
     enddo

     ! TEB_SPM
     if (TEB_SPM==1) then
        ! read FUSO (Local Time) files
        do ifm = 1,ngrids
           call fuso_read(ifm)
        enddo
     endif

     ! The following things will be done for INITAIL = 1 or 3...

     if ( initial == 1 .or. initial == 3) then

        ! If horizontally homogeneous initialization,
        !    subroutine INITHH loops through all grids and initializes
        !    those for which nxtnest = 0.

        if(initial == 1) then
           print*,'Horizontally-homogeneous-INITIAL start of grid- 1'
           call inithh()
        endif

        !If "history" initialization, call INITHIS.
        !This will define initial fields and reference state on grid 1 from
        !history file. Other grids will be interpolated as in a INITIAL=1 start

        if (initial == 3) then
           print*,'History-INITIAL start of grid- 1'
           call inithis()
        endif


        !  On all fine grids, initialize the surface layer characteristics,
        !  the 1-D reference state arrays, the 3-D reference state arrays,
        !  and the prognostic atmospheric fields by interpolation.

        call fmrefs1d(2,ngrids)

        do ifm = 2,ngrids
           icm = nxtnest(ifm)
           if (icm  >=  1) then
              call fmrefs3d(ifm)

              call prgintrp(nnzp(icm),nnxp(icm),nnyp(icm)  &
                   ,nnzp(icm),nnxp(icm),nnyp(icm),0,0,ifm,1,mynum)

              call fmdn0(ifm)

              print*,'Initial interpolation of grid-',ifm
           endif
        enddo


     elseif(initial == 2) then

        ! If "variable initialization", do it all here

        call varf_read(0)

     endif

     !     Initialize past time level velocity and perturbation Exner function
     !     on all grids.

     do ifm=1,ngrids
        call newgrid(ifm)
        call fldinit(1)
        call negadj1(nzp,nxp,nyp)
        call thermo(nzp,nxp,nyp,1,nxp,1,nyp,'THRM_ONLY')

        if (level  ==  3) then
           call initqin(nzp,nxp,nyp        &
                ,micro_g(ifm)%q2    (1,1,1)  &
                ,micro_g(ifm)%q6    (1,1,1)  &
                ,micro_g(ifm)%q7    (1,1,1)  &
                ,basic_g(ifm)%pi0   (1,1,1)  &
                ,basic_g(ifm)%pp    (1,1,1)  &
                ,basic_g(ifm)%theta (1,1,1)  &
                ,basic_g(ifm)%dn0   (1,1,1)  &
                ,micro_g(ifm)%cccnp (1,1,1)  &
                ,micro_g(ifm)%cifnp (1,1,1)  )

           ! This section only if bin model spawned from bulk model fields
           !            elseif (level  ==  4or5) then
           !               call xmic_init(nzp,nxp,nyp,nb  &
           !                 ,basic_g(ifm)%dn0 (1,1,1)  &
           !                 ,basic_g(ifm)%rtp (1,1,1)  &
           !                 ,basic_g(ifm)%rv  (1,1,1)  &
           !                 ,sclp(2))  &
           !                 ,sclp(nb+2))  &
           !                 ,vctr1,vctr2)
        endif

     enddo

     ! If initializing some fields from previous runs...

     ! Do recycle procedure for normal RAMS recycle or only to do
     ! tracers assimilation
     if ((ipastin == 1) .or. (CATT==1 .and. RECYCLE_TRACERS==1)) then
        call recycle()
     endif

     ! Fill land surface data for all grids that have no standard input files

     ! ALF - For use with SiB
     if (isfcl <= 2) then
        call sfcdata
     elseif (isfcl == 3) then
        call sfcdata_sib_driver
     endif


     ! Initialize various LEAF variables.
     if (ipastin == 0) call geonest_nofile(1,ngrids)

     ! TEB
     if (TEB_SPM==1) then

        ! Initial values for Common use TEB vars 
        ! For now, it is only being used to zero out this four common 
        ! use variables.
        ! This variables will be used for other purposes later.
        ! Edmilson D. Freitas 07/07/2006

        do ifm = 1,ngrids
           call TEBC_INIT(ifm,nnxp(ifm),nnyp(ifm),npatch &
                ,leaf_g(ifm)%leaf_class   (1,1,1)        &
                ,leaf_g(ifm)%G_URBAN      (1,1,1)        &
                ,tebc_g(ifm)%EMIS_TOWN    (1,1)          &
                ,tebc_g(ifm)%ALB_TOWN     (1,1)          &
                ,tebc_g(ifm)%TS_TOWN      (1,1)          )
        enddo

        if (iteb==1) then
           do ifm = 1,ngrids
              call TEB_INIT(ifm,nnzp(ifm),nnxp(ifm),nnyp(ifm),npatch         &
                   ,leaf_g(ifm)%leaf_class(1,1,1),basic_g(ifm)%theta  (1,1,1)&
                   ,basic_g(ifm)%rv       (1,1,1),basic_g(ifm)%pi0    (1,1,1)&
                   ,basic_g(ifm)%pp       (1,1,1)                            &
                   ,teb_g(ifm)%T_ROOF     (1,1,1),teb_g(ifm)%T_ROAD   (1,1,1)&
                   ,teb_g(ifm)%T_WALL     (1,1,1),teb_g(ifm)%TI_BLD     (1,1)&
                   ,teb_g(ifm)%TI_ROAD    (1,1)  ,teb_g(ifm)%T_CANYON   (1,1)&
                   ,teb_g(ifm)%R_CANYON   (1,1)  ,teb_g(ifm)%TS_ROOF    (1,1)&
                   ,teb_g(ifm)%TS_ROAD    (1,1)  ,teb_g(ifm)%TS_WALL    (1,1)&
                   ,teb_g(ifm)%H_TRAFFIC  (1,1)  ,teb_g(ifm)%LE_TRAFFIC (1,1)&
                   ,teb_g(ifm)%H_INDUSTRY (1,1)  ,teb_g(ifm)%LE_INDUSTRY(1,1)&
                   ,teb_g(ifm)%WS_ROOF    (1,1)  ,teb_g(ifm)%WS_ROAD    (1,1)&
                   ,tebc_g(ifm)%EMIS_TOWN (1,1)  ,tebc_g(ifm)%ALB_TOWN  (1,1)&
                   ,tebc_g(ifm)%TS_TOWN   (1,1)  ,leaf_g(ifm)%G_URBAN   (1,1,1))
           enddo
        endif

        ! Initialize gases and particulate matter
        if (isource==1) then
           do ifm = 1,ngrids
              call init_conc1(1,ifm,nnzp(ifm),nnxp(ifm),nnyp(ifm),npatch    &
                   ,leaf_g(ifm)%G_URBAN(1,1,1)  ,gaspart_g(ifm)%pno(1,1,1)  &
                   ,gaspart_g(ifm)%pno2(1,1,1)  ,gaspart_g(ifm)%ppm25(1,1,1)&
                   ,gaspart_g(ifm)%pco(1,1,1)   ,gaspart_g(ifm)%pvoc(1,1,1) & 
                   ,gaspart_g(ifm)%pso2(1,1,1)  ,gaspart_g(ifm)%pso4(1,1,1) &
                   ,gaspart_g(ifm)%paer(1,1,1)  ,zt                         )
              if (ichemi==1) then       !calling more added scalars for chemistry
                 if (ichemi_in==1) then !reading init.values from a previous run
                    call init_conc_prev(name_name)
                 else
                    call init_conc2(1,ifm,nnzp(ifm),nnxp(ifm),nnyp(ifm),npatch,&
                         leaf_g(ifm)%G_URBAN(1,1,1),gaspart_g(ifm)%po3(1,1,1), &
                         gaspart_g(ifm)%prhco(1,1,1),                          &
                         gaspart_g(ifm)%pho2(1,1,1),                           &
                         gaspart_g(ifm)%po3p(1,1,1),gaspart_g(ifm)%po1d(1,1,1),&
                         gaspart_g(ifm)%pho(1,1,1),gaspart_g(ifm)%proo(1,1,1), &
                         zt)
                 endif
              endif
           enddo
        endif

     endif

!--(DMK-CCATT)---------------------------------------------------------
     ! CATT - CHEMISTRY
     !-srf  Initialize the true air density
     if (iexev == 2) then
         do ifm=1,ngrids
     	    call newgrid(ifm)
     	    stilt_g(ifm)%dnp(:,:,:)= basic_g(ifm)%dn0(:,:,:)
         enddo
     endif
     if (CATT == 1 ) then
        do ifm=1,ngrids
           !srf- initialize akmin = f(x,y)
           if(AKMIN(ifm) .ge. 0.) cycle
	   call newgrid(ifm)
	   call get_akmin2d(ifm,nxp,nyp)
	enddo
     endif
     if (CATT == 1 .and.  CHEMISTRY >= 0 ) then
        !srf- initialize dry-dep constants
	call dep_init   
        !-srf initialize mixing ratios (only if chem assim is off) 
	!- and sources
        do ifm=1,ngrids
           call newgrid(ifm)
	   call initial_condition(ifm,nzp,nxp,nyp)
           call le_sourcemaps(ifm,nzp,nxp,nyp,time,iyear1,imonth1,idate1,itime1,ngrids,timmax, &
                              chem_nspecies,spc_chem_alloc,src,off,nsrc,nvert_src,chem1_src_g, &
                              bburn,spc_chem_name,on,chemical_mechanism,emiss_ajust,co,        &
                              aer_nspecies,spc_aer_alloc,spc_aer_name,urban,nucle,accum,       &
                              src_name,chemistry,ntimes_src,aer1_g,nmodes,aerosol,nveg_agreg,  &
                              plume_mean_g,nzpmax,dzt,grid_g(ifm)%rtgt,transport,aer_bburn,    &
                              plume_g,tropical_forest,boreal_forest,savannah,grassland,diur_cycle)

!           call le_sourcemaps(ifm,nzp,nxp,nyp) 
	enddo
     end if
!--(DMK-original)------------------------------------------------------
!     ! Read Radiation Parameters if CARMA Radiation is selected
!     call master_read_carma_data()
!     !
!     ! CATT
!     if (CATT==1) then
!        ! srf......................................................
!        ! Initiating tracers concentration and sources
!        if (RECYCLE_TRACERS==0) then
!           do ifm=1,NGRIDS
!              ! Do not perform recycle procedure for tracers
!              ! Initiating with zeros
!              ! Putting zero on specific scalar vectors
!              scalar_g(1,ifm)%sclp(:,:,:) = 0.
!              scalar_g(2,ifm)%sclp(:,:,:) = 0.
!              scalar_g(3,ifm)%sclp(:,:,:) = 0.
!              scalar_g(4,ifm)%sclp(:,:,:) = 0.
!              !!scalar_g(5,ifm)%sclp(:,:,:) = 0.
!           enddo
!        endif
!        ! Reading Emission Maps for all grids
!        call read_emission_sources_map()
!     endif
!--(DMK-CCATT-END)-----------------------------------------------------


     if (initial == 3) call sfcinit_hstart()

     !hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

  elseif(runtype(1:7) == 'HISTORY') then

     !hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh

     !                  History file start
     !                  -----------------

     call history_start(name_name)

     call grid_setup(1)

     ! Check surface,topo,sst,ndvi files. Remake if necessary.

     call make_sfcfiles()

     ! Read surface and topo files for any added grids

     do ifm = ngridsh+1,ngrids
        call sfc_read(ifm)
     enddo

     do ifm = ngridsh+1,ngrids
        call top_read(ifm)
     enddo

     call grid_setup(2)

     ! Read in sst and ndvi files for all grids

     do ifm = 1,ngrids
        call sst_read(1,ifm,ierr)
     enddo

     do ifm = 1,ngrids
        call ndvi_read(1,ifm,ierr)
     enddo

     ! TEB
     if (TEB_SPM==1) then
        ! Read FUSO (Local Time) files for any added grids
        do ifm = ngridsh+1,ngrids
           call fuso_read(ifm)
        enddo
     endif

     do ifm = 1,ngrids
        icm = nxtnest(ifm)
        if (icm  ==  0) then
           call newgrid(ifm)
           call refs3d (nzp,nxp,nyp  &
                ,basic_g(ifm)%pi0  (1,1,1),basic_g(ifm)%dn0  (1,1,1)  &
                ,basic_g(ifm)%dn0u (1,1,1),basic_g(ifm)%dn0v (1,1,1)  &
                ,basic_g(ifm)%th0  (1,1,1),grid_g(ifm)%topt  (1,1)    &
                ,grid_g(ifm)%rtgt  (1,1)  )
        endif
     enddo

     do ifm = 1,min(ngrids,ngridsh)
        icm = nxtnest(ifm)
        if (icm  >  0) call fmrefs3d(ifm)
        call negadj1(nzp,nxp,nyp)
     enddo

!--(DMK-CCATT)---------------------------------------------------------
     !srf mod rams60
     ! If initializing some fields from previous runs...
     if (ipastin == 1) call recycle()
     !srf mod rams60
!--(DMK-CCATT-END)-----------------------------------------------------

     ! ALF - For use with SiB
     if (isfcl <= 2) then
        call sfcdata
     elseif (isfcl == 3) then
        call sfcdata_sib_driver
     endif

     ! Heterogenous Soil Moisture Init.
     if ((SOIL_MOIST == 'h').or.(SOIL_MOIST == 'H').or.  &
          (SOIL_MOIST == 'a').or.(SOIL_MOIST == 'A')) then

        do ifm = 1,min(ngrids,ngridsh)
           call newgrid(ifm)
           call soil_moisture_init(nnzp(ifm),nnxp(ifm),nnyp(ifm)       &
                ,nzg,nzs,npatch,ifm              &
                ,basic_g(ifm)%theta (1,1,1) ,basic_g(ifm)%pi0 (1,1,1)  &
                ,basic_g(ifm)%pp    (1,1,1)  &
                ,leaf_g(ifm)%soil_water      (1,1,1,1)  &
                ,leaf_g(ifm)%soil_energy     (1,1,1,1)  &
                ,leaf_g(ifm)%soil_text       (1,1,1,1)  &
                ,leaf_g(ifm)%sfcwater_mass   (1,1,1,1)  &
                ,leaf_g(ifm)%sfcwater_energy (1,1,1,1)  &
                ,leaf_g(ifm)%sfcwater_depth  (1,1,1,1)  &
                ,grid_g(ifm)%glat   (1,1)       &
                ,grid_g(ifm)%glon (1,1)         &
                ,grid_g(ifm)%lpw (1,1)        )
        enddo

     endif

     !     If any grids are being added for this run, initialize their
     !     surface layer variables, 1-D reference state variables, and
     !     prognostic atmospheric and soil model fields.

     if (ngrids  >  ngridsh) then
        print*,' +-------------------------------------'
        print*,'            !      New grids will be added.       '
        print*,'            !'
        print*,'            ! ',ngridsh,' grid(s) on history file.'
        print*,'            ! ',ngrids, ' grids to be run.        '
        print*,' +-------------------------------------'
        call fmrefs1d(ngridsh+1,ngrids)
        do ifm = ngridsh+1,ngrids
           icm = nxtnest(ifm)
           if (icm  ==  0) then
              call fatal_error(h//"Attempted to add "//&
                   "a hemispheric grid on a history restart; "//&
                   "this cannot be done.")
           endif
           call fmrefs3d(ifm)
           call prgintrp(nnzp(icm),nnxp(icm),nnyp(icm)  &
                ,nnzp(icm),nnxp(icm),nnyp(icm),0,0,ifm,1,mynum)
           print*,'History start interpolation of added grid-',ngrid

           call fmdn0(ifm)
           call newgrid(ifm)
           call fldinit(0)
           call negadj1(nzp,nxp,nyp)
           call thermo(nzp,nxp,nyp,1,nxp,1,nyp,'THRM_ONLY')
           if (level  ==  3) then
              call initqin(nzp,nxp,nyp        &
                   ,micro_g(ifm)%q2    (1,1,1)  &
                   ,micro_g(ifm)%q6    (1,1,1)  &
                   ,micro_g(ifm)%q7    (1,1,1)  &
                   ,basic_g(ifm)%pi0   (1,1,1)  &
                   ,basic_g(ifm)%pp    (1,1,1)  &
                   ,basic_g(ifm)%theta (1,1,1)  &
                   ,basic_g(ifm)%dn0   (1,1,1)  &
                   ,micro_g(ifm)%cccnp (1,1,1)  &
                   ,micro_g(ifm)%cifnp (1,1,1)  )

           endif

           ! Heterogenous Soil Moisture Init.
           if ((SOIL_MOIST == 'h').or.(SOIL_MOIST == 'H').or.  &
              (SOIL_MOIST == 'a').or.(SOIL_MOIST == 'A')) then
              call soil_moisture_init(nnzp(ifm),nnxp(ifm),nnyp(ifm)       &
                   ,nzg,nzs,npatch,ifm              &
                   ,basic_g(ifm)%theta (1,1,1) ,basic_g(ifm)%pi0 (1,1,1)  &
                   ,basic_g(ifm)%pp    (1,1,1)  &
                   ,leaf_g(ifm)%soil_water      (1,1,1,1)  &
                   ,leaf_g(ifm)%soil_energy     (1,1,1,1)  &
                   ,leaf_g(ifm)%soil_text       (1,1,1,1)  &
                   ,leaf_g(ifm)%sfcwater_mass   (1,1,1,1)  &
                   ,leaf_g(ifm)%sfcwater_energy (1,1,1,1)  &
                   ,leaf_g(ifm)%sfcwater_depth  (1,1,1,1)  &
                   ,grid_g(ifm)%glat   (1,1)       &
                   ,grid_g(ifm)%glon (1,1)         &
                   ,grid_g(ifm)%lpw (1,1)        )
           endif

        enddo

        !Fill land surface data for all grids that have no standard input files
        call geonest_nofile(ngridsh+1,ngrids)

     elseif (ngrids  <  ngridsh) then
        print*,' +-------------------------------------'
        print*,'            !      Grids will be subtracted.       '
        print*,'            !'
        print*,'            ! ',NGRIDSH,' grid(s) on history file.'
        print*,'            ! ',NGRIDS, ' grids to be run.        '
        print*,' +-------------------------------------'
     endif

!--(DMK-CCATT)---------------------------------------------------------
     if (CATT == 1 ) then
        do ifm=1,ngrids
           !initialize akmin = f(x,y)
           call newgrid(ifm)
	   call get_akmin2d(ifm,nxp,nyp)
	enddo
     endif
     if (CATT == 1 .and.  CHEMISTRY >= 0 ) then
        !srf......................................................
        !inicialise dry-dep constantes
	call dep_init
	!Reinicialize  as fontes	
        do ifm=1,NGRIDS
           call NEWGRID(ifm)
           !call le_firemaps(ifm,nzp,nxp,nyp)
           call le_sourcemaps(ifm,nzp,nxp,nyp,time,iyear1,imonth1,idate1,itime1,ngrids,timmax, &
                              chem_nspecies,spc_chem_alloc,src,off,nsrc,nvert_src,chem1_src_g, &
                              bburn,spc_chem_name,on,chemical_mechanism,emiss_ajust,co,        &
                              aer_nspecies,spc_aer_alloc,spc_aer_name,urban,nucle,accum,       &
                              src_name,chemistry,ntimes_src,aer1_g,nmodes,aerosol,nveg_agreg,  &
                              plume_mean_g,nzpmax,dzt,grid_g(ifm)%rtgt,transport,aer_bburn,    &
                              plume_g,tropical_forest,boreal_forest,savannah,grassland,diur_cycle)

!           call le_sourcemaps(ifm,nzp,nxp,nyp) !ifm eh a grade atual

        enddo
        !srf......................................................
     endif
!--(DMK-original)------------------------------------------------------
!     ! Read Radiation Parameters if CARMA Radiation is selected
!     call master_read_carma_data()
!     !
!     ! CATT
!     if (CATT==1) then
!        !srf......................................................
!        ! Reading Emission Maps for all grids
!        call read_emission_sources_map()
!     endif
!--(DMK-CCATT-END)-----------------------------------------------------

  else
     call fatal_error(h//" wrong runtype in initlz")

  endif

  ! For a global model domain, initialize interpolation table values for
  ! communication between hemispheric grids.  Subroutine hemintrp_cof will
  ! return immediately if nhemgrd2 is not greater than 1.

  call newgrid(1)
  ihm = nnxyp(1)
  call hemintrp_cof (nnxp(1),nnyp(1)  &
       ,scratch%scr1(1)      ,scratch%scr1(1+ihm)  ,scratch%scr1(1+2*ihm)  &
       ,scratch%scr1(1+3*ihm),scratch%scr1(1+4*ihm),scratch%scr1(1+5*ihm))

  call micro_master()

  !       Fill latitude-longitude, map factor, and Coriolis arrays.

  do ifm = 1,ngrids
     call newgrid(ifm)
     call fcorio(nxp,nyp           &
          ,basic_g(ifm)%fcoru (1,1)  &
          ,basic_g(ifm)%fcorv (1,1)  &
          ,grid_g(ifm)%glat   (1,1)  )
  enddo


  !  If we are doing one-way nesting or varfile nudging, inventory,
  !     prepare history/varfile files
  !     and fill past/future nudging arrays for start of simulation

  if ( nud_type == 1 ) then
     call nud_read(1)
  elseif(nud_type == 2) then
     call varf_read(1)
  endif

  ! Do same if doing condensate nudging

  if ( nud_cond == 1 ) call cond_read(1)

  ! Process and read observations for ODA - observational data assimilation

  if (if_oda == 1) call oda_read()

  ! Read cumulus heating fields

  if (if_cuinv == 1) call cu_read(1)

  ! Initialize urban canopy drag coefficients

  if (if_urban_canopy == 1) call urb_drag_init()


  ! Print locations of all grids

  call gridloc_prt()

  !                  Save initial fields on history and analysis files
  !                  -------------------------------------------------
  iinput=ioutput
  rest  ='no'
  if (runtype  ==  'HISTORY') rest = 'yes'
  call hiswrt(rest)
  call anlwrt(rest,'INST')
  if(frqlite > 0.) call anlwrt(rest,'LITE')

  !                  Save initial fields into the averaged arrays
  !                  -------------------------------------------------
  if(avgtim /= 0.)then

     do ngr=1,ngrids

        do nv=1,num_var(ngr)
           if(vtab_r(nv,ngr)%imean == 1) then
              call atob(vtab_r(nv,ngr)%npts,vtab_r(nv,ngr)%var_p  &
                   ,vtab_r(nv,ngr)%var_m)
           endif
        enddo
     enddo
  endif


  !                  Print the header information and initial fields
  !                  -----------------------------------------------
  ngrid=1

  call prtopt(6)

!!$  call nameout()

  if (initfld  ==  1) then
     do ifm = 1,ngrids
        call newgrid(ifm)
        call prtout()
     enddo
  endif

  call opspec3()

  return
end subroutine initlz

!*****************************************************************************
! PPL - begin of NAMELIST read
subroutine read_nl(file)

  ! read_nl:
  !   reads namelist and checks consistency of a few options

  use mem_grid, only: &
       timeunit,      &
       timmax

  use io_params, only: &
       timstr

  implicit none

  character(len=*), intent(in) :: file
  character(len=*), parameter :: h="**(read_nl)**"

  real :: tfact

  ! Deactivate namelist scanning and parsing
  ! Activate standard (fortran 90) namelist read
  ! Estabilishes default options and option consistency

  call ReadNamelist(file)

  !         Change some input time specifications into seconds

  if(timeunit == 'd'.or.timeunit == 'D') tfact=86400.
  if(timeunit == 'h'.or.timeunit == 'H') tfact=3600.
  if(timeunit == 'm'.or.timeunit == 'M') tfact=60.
  if(timeunit == 's'.or.timeunit == 'S') tfact=1.

  timmax=timmax*tfact
  timstr=timstr*tfact
end subroutine read_nl



subroutine ReadNamelist(fileName)

  ! ReadNamelist:
  !    open, reads and close namelist file
  !    implements defaults for namelist variables
  !    check input options consistency

  use io_params, only: frqboth, &
       afilout,                 &
       avgtim,                  &
       frqanl,                  &
       frqhis,                  &
       frqlite,                 &
       frqmean,                 &
       frqprt,                  &
       hfilin,                  &
       hfilout,                 &
       iclobber,                &
       ihistdel,                &
       initfld,                 &
       prtcputime,              &
       ioutput,                 &
       ipastin,                 &
       iplfld,                  &
       isbval,                  &
       isoilflg,                &
       isoilfn,                 &
       isstflg,                 &
       isstfn,                  &
       itopsflg,                &
       itoptflg,                &
       itoptfn,                 &
       iupdndvi,                &
       iupdsst,                 &
       ivegtflg,                &
       ivegtfn,                 &
       ixsctn,                  &
       iz0flg,                  &
       kwrite,                  &
       lite_vars,               &
       ndviflg,                 &
       ndvifn,                  &
       ndvifpfx,                &
       nlite_vars,              &
       nofilflg,                &
       nplt,                    &
       pastfn,                  &
       sfcfiles,                &
       sstfpfx,                 &
       timstr,                  &
       topfiles,                &
       toptenh,                 &
       toptwvl,                 &
       xlite,                   &
       ylite,                   &
       z0fact,                  &
       z0max,                   &
       zlite,                   &
       ! TEB
       ifusflg,                 &
       ifusfn,                  &
       fusfiles
  use isan_coms, only: gobrad, &
       gobsep, &
       gridwt, &
       guess1st, &
       hybbot, &
       hybtop, &
       i1st_flg, &
       iapr, &
       iarawi, &
       iasrfce, &
       igridfl, &
       iobswin, &
       ioflgisz, &
       ioflgvar, &
       isan_inc, &
       isfc_flg, &
       iszstage, &
       iupa_flg, &
       ivrstage, &
       levth, &
       maxsfc, &
       maxsta, &
       nfeedvar, &
       nigrids, &
       nisn, &
       notid, &
       notsta, &
       respon, &
       sfcinf, &
       sigzwt, &
       stasep, &
       swvlnth, &
       topsigz, &
       varpfx, &
       wvlnth


!--(DMK-CCATT)---------------------------------------------------------
  !srf - chem
  use chem_sources, only: SRCMAPFN, &
       plumerise, &
       prfrq,     &
       def_proc_src
  use mem_chem1, only: RECYCLE_TRACERS, &
       CHEM_ASSIM, &
       CHEMISTRY,  &
       CHEM_TIMESTEP, &
       split_method,&
       diur_cycle, &
       nsrc
  use mem_chem1aq, only: CHEMISTRY_AQ
  use mem_aer1, only: AEROSOL
  !srf - chem - end
  ! For STILT
  use mem_stilt, only : iexev, imassflx
!--(DMK-CCATT-END)-----------------------------------------------------


  use mem_cuparm, only: confrq, &
       cu_prefix, &
       cu_tel, &
       cu_til, &
       if_cuinv, &
       nnqparm, &
       tcu_beg, &
       tcu_end, &
       tnudcu, &
       wcldbs, &
       wt_cu_grid
  use mem_globrad, only: raddatfn, & !CARMA
       rdatfnum                      !CARMA
  use mem_grell_param, only: closure_type
  use mem_grid, only: centlat, &
       centlon, &
       cphas, &
       deltax, &
       deltay, &
       deltaz, &
       distim, &
       dtlong, &
       dzmax, &
       dzrat, &
       expnme, &
       gridu, &
       gridv, &
       ibnd, &
       icorflg, &
       idate1, &
       ideltat, &
       if_adap, &
       ihtran, &
       imonth1, &
       initial, &
       itime1, &
       iyear1, &
       jbnd, &
       lsflg, &
       nacoust, &
       naddsc, &
       nestz1, &
       nestz2, &
       nfpt, &
       ngrids, &
       ninest, &
       njnest, &
       nknest, &
       nndtrat, &
       nnstbot, &
       nnsttop, &
       nnxp, &
       nnyp, &
       nnzp, &
       npatch, &
       nstratx, &
       nstraty, &
       nstratz1, &
       nstratz2, &
       nxtnest, &
       nzg, &
       nzs, &
       polelat, &
       polelon, &
       runtype, &
       timeunit, &
       timmax, &
       zz, &

!--(DMK-CCATT)---------------------------------------------------------
       STDLAT1,&
       STDLAT2
!--(DMK-CCATT-END)-----------------------------------------------------

  use mem_leaf, only: albedo, &
       drtcon, &
       dthcon, &
       isfcl, &
       nslcon, &
       nvegpat, &
       nvgcon, &
       pctlcon, &
       seatmp, &
       slmstr, &
       slz, &
       stgoff, &
       zrough
  use mem_oda, only: frqoda, &
       if_oda, &
       oda_sfc_tel, &
       oda_sfc_til, &
       oda_sfcprefix, &
       oda_upa_tel, &
       oda_upa_til, &
       oda_upaprefix, &
       roda_hgt, &
       roda_sfc0, &
       roda_sfce, &
       roda_upa0, &
       roda_upae, &
       roda_zfact, &
       tnudoda, &
       todabeg, &
       todaend, &
       wt_oda_grid, &
       wt_oda_pi, &
       wt_oda_rt, &
       wt_oda_th, &
       wt_oda_uv
  use mem_radiate, only: ilwrtyp, &
       iswrtyp, &
       lonrad, &
       radfrq
  use mem_soil_moisture, only: soil_moist, &
       soil_moist_fail, &
       usdata_in, &
       usmodel_in
  use mem_turb, only: akmin, &
       csx, &
       csz, &
       idiffk, &
       if_urban_canopy, &
       ihorgrad, &
       xkhkm, &
       zkhkm
  use mem_varinit, only: cond_hfile, &
       nud_cond, &
       nud_hfile, &
       nud_type, &
       nudlat, &
       t_nudge_rc, &
       tcond_beg, &
       tcond_end, &
       tnudcent, &
       tnudlat, &
       tnudtop, &
       varfpfx, &
       vwait1, &
       vwaittot, &
       wt_nudge_grid, &
       wt_nudge_pi, &
       wt_nudge_rt, &
       wt_nudge_th, &
       wt_nudge_uv, &
       wt_nudgec_grid, &
       znudtop
  use micphys, only: aparm, &
       coltabfn, &
       cparm, &
       gnu, &
       gparm, &
       hparm, &
       iaggr, &
       icloud, &
       igraup, &
       ihail, &
       ipris, &
       irain, &
       isnow, &
       level, &
       mkcoltab, &
       pparm, &
       rparm, &
       sparm
  use node_mod, only: load_bal
  use ref_sounding, only: hs, &
       ipsflg, &
       irtsflg, &
       itsflg, &
       iusflg, &
       ps, &
       rts, &
       ts, &
       us, &
       vs
  use shcu_vars_const, only: nnshcu, &
       shcufrq
  use sib_vars, only: co2_init, &
       n_co2

  ! CATT
  use catt_start, only: CATT

!--(DMK-CCATT)---------------------------------------------------------
!--(DMK-original)------------------------------------------------------
!  use emission_source_map, only: firemapfn, &
!       tracersfn,                           &
!       plumerise
!  use plume_utils, only: prfrq
!  use mem_scalar, only: RECYCLE_TRACERS ! CATT
!--(DMK-CCATT-END)-----------------------------------------------------

  !TEB_SPM
  use teb_spm_start, only: TEB_SPM

  use mem_emiss, only : &
       ichemi,          &
       ichemi_in,       &
       chemdata_in,     &
       isource,         &
       weekdayin,       &
       efsat,           &
       efsun,           &
       eindno,          &
       eindno2,         &
       eindpm,          &
       eindco,          &
       eindso2,         &
       eindvoc,         &
       eveino,          &
       eveino2,         &
       eveipm,          &
       eveico,          &
       eveiso2,         &
       eveivoc

  use teb_vars_const, only : &
       rushh1,               &
       rushh2,               &
       daylight,             &
       iteb,                 &
       tminbld,              &
       nteb,                 &
       hc_roof,              &
       tc_roof,              &
       d_roof,               &
       hc_road,              &
       d_road,               &
       tc_road,              &
       d_wall,               &
       tc_wall,              &
       hc_wall,              &
       nurbtype,             &
       ileafcod,             &
       z0_town,              &
       bld,                  &
       bld_height,           &
       bld_hl_ratio,         &
       aroof,                &
       eroof,                &
       aroad,                &
       eroad,                &
       awall,                &
       ewall,                &
       htraf,                &
       hindu,                &
       pletraf,              &
       pleindu

  use grid_dims, only: &
       maxsteb,        &
       maxubtp

  ! Explicit domain decomposition
  use domain_decomp, only: domain_fname

  use extras, only: na_extra2d, & ! INTENT(IN)
                    na_extra3d                 ! INTENT(IN)


  implicit none

  character(len=*), intent(in) :: fileName  ! file name with namelists

  integer :: i                        ! loop count
  integer :: iunit                    ! io unit number
  integer, parameter :: firstUnit=20  ! lowest io unit number available
  integer, parameter :: lastUnit=99   ! highest io unit number available
  logical :: op                       ! io unit number opened or not
  logical :: ex                       ! namelist file exists?
  integer :: err                      ! return code on iostat
  character(len=10) :: c0             ! scratch
  character(len=*), parameter :: h="**(ReadNamelist)**"  ! program unit name

  namelist /MODEL_GRIDS/                                               &
       expnme, runtype, timeunit, timmax, load_bal, imonth1, idate1,   &
       iyear1, itime1, ngrids, nnxp, nnyp, nnzp, nzg, nzs, nxtnest,    &
       domain_fname,                                                   &
       if_adap, ihtran, deltax, deltay, deltaz, dzrat, dzmax, zz,      &
       dtlong, nacoust, ideltat, nstratx, nstraty, nndtrat, nestz1,    &
       nstratz1, nestz2, nstratz2, polelat, polelon, centlat, centlon, &
       ninest, njnest, nknest, nnsttop, nnstbot, gridu, gridv,         &

!--(DMK-CCATT)---------------------------------------------------------
       STDLAT1, STDLAT2
!--(DMK-CCATT-END)-----------------------------------------------------


  namelist /CATT_INFO/                                                 &
!--(DMK-CCATT)---------------------------------------------------------
       CATT,                                                           &
       srcmapfn, recycle_tracers,  chem_assim, chemistry, chemistry_aq,&
       aerosol,  chem_timestep,split_method,def_proc_src,diur_cycle,   &
       na_extra2d, na_extra3d,                                         &
       plumerise,prfrq
!--(DMK-CCATT-END)-----------------------------------------------------

  namelist /TEB_SPM_INFO/                                              &
       teb_spm,                                                        &
       fusfiles, ifusflg, ifusfn,                                      &
       ichemi, ichemi_in, chemdata_in, isource, weekdayin, rushh1,     &
       rushh2, daylight, efsat, efsun, eindno, eindno2, eindpm,        &
       eindco, eindso2, eindvoc, eveino, eveino2, eveipm, eveico,      &
       eveiso2, eveivoc, iteb, tminbld, nteb, hc_roof, tc_roof,        &
       d_roof, hc_road, tc_road, d_road, hc_wall, tc_wall, d_wall,     &
       nurbtype, ileafcod, z0_town, bld, bld_height, bld_hl_ratio,     &
       aroof, eroof, aroad, eroad, awall, ewall, htraf, hindu,         &
       pletraf, pleindu

  namelist /MODEL_FILE_INFO/                                           &
       initial, nud_type, varfpfx, vwait1, vwaittot, nud_hfile, nudlat,&
       tnudlat, tnudcent, tnudtop, znudtop, wt_nudge_grid, wt_nudge_uv,&
       wt_nudge_th, wt_nudge_pi, wt_nudge_rt, nud_cond, cond_hfile,    &
       tcond_beg, tcond_end, t_nudge_rc, wt_nudgec_grid, if_oda,       &
       oda_upaprefix,oda_sfcprefix, frqoda, todabeg, todaend, tnudoda, &
       wt_oda_grid, wt_oda_uv, wt_oda_th, wt_oda_pi, wt_oda_rt,        &
       roda_sfce, roda_sfc0, roda_upae,roda_upa0, roda_hgt,            &
       roda_zfact, oda_sfc_til, oda_sfc_tel, oda_upa_til, oda_upa_tel, &
       if_cuinv, cu_prefix, tnudcu, wt_cu_grid, tcu_beg, tcu_end,      &
       cu_tel, cu_til, timstr, hfilin, ipastin, pastfn, ioutput,       &
       hfilout, afilout, iclobber, ihistdel, frqhis, frqanl, frqlite,  &
       xlite, ylite, zlite, nlite_vars, lite_vars, avgtim, frqmean,    &
       frqboth, kwrite, frqprt, initfld, prtcputime, topfiles,         &
       sfcfiles, sstfpfx, ndvifpfx, itoptflg, isstflg, ivegtflg,       &
       isoilflg, ndviflg, nofilflg, iupdndvi, iupdsst, itoptfn, isstfn,&
       ivegtfn, isoilfn, ndvifn, itopsflg, toptenh, toptwvl, iz0flg,   &
       z0max, z0fact, mkcoltab, coltabfn

  namelist /MODEL_OPTIONS/ &
       naddsc, icorflg, ibnd, jbnd, cphas, lsflg, nfpt, distim,          &
       iswrtyp, ilwrtyp,                                                 &
       raddatfn,                                                         &
       radfrq, lonrad, nnqparm, closure_type, nnshcu, confrq,            &
       shcufrq, wcldbs, npatch, nvegpat, isfcl, n_co2, co2_init, nvgcon, &
       pctlcon, nslcon, drtcon, zrough, albedo, seatmp, dthcon,          &
       soil_moist, soil_moist_fail, usdata_in, usmodel_in, slz, slmstr,  &
       stgoff, if_urban_canopy, idiffk, ihorgrad, csx, csz, xkhkm, zkhkm,&
       akmin, level, icloud, irain, ipris, isnow, iaggr, igraup, ihail,  &
       cparm, rparm, pparm, sparm, aparm, gparm, hparm,                  &
       gnu,                                                              &

!--(DMK-CCATT)---------------------------------------------------------
       !ml/srf - for stilt and mass conservation
       iexev, imassflx
!--(DMK-CCATT-END)-----------------------------------------------------


  namelist /MODEL_SOUND/ &
       ipsflg, itsflg, irtsflg, iusflg, hs, ps, ts, rts, us, vs

  namelist /MODEL_PRINT/ &
       nplt, iplfld, ixsctn, isbval

  namelist /ISAN_CONTROL/ &
       iszstage, ivrstage, isan_inc, guess1st, i1st_flg, iupa_flg,       &
       isfc_flg, iapr, iarawi, iasrfce, varpfx, ioflgisz, ioflgvar

  namelist /ISAN_ISENTROPIC/ &
       nisn, levth, nigrids, topsigz, hybbot, hybtop, sfcinf, sigzwt,    &
       nfeedvar, maxsta, maxsfc, notsta, notid, iobswin, stasep, igridfl,&
       gridwt, gobsep, gobrad, wvlnth, swvlnth, respon


  ! defaults (just for arrays); should be revised to accomodate
  ! precise defaults
!PPL  nnxp = 0
!PPL  nnyp = 0
!PPL  nnzp = 0
!PPL  centlat = 0.0
!PPL  centlon = 0.0
!PPL  nxtnest = 0
!PPL  zz = 0.0
!PPL  nstratx = 0
!PPL  nstraty = 0
!PPL  nndtrat = 0
!PPL  nstratz1 = 0
!PPL  nstratz2 = 0
!PPL  ninest = 0
!PPL  njnest = 0
!PPL  nknest = 0
!PPL  nnsttop = 0
!PPL  nnstbot = 0
!PPL  gridu = 0.0
!PPL  gridv = 0.0
!PPL  wt_nudge_grid = 0.0
!PPL  wt_nudgec_grid = 0.0
!PPL  wt_oda_grid = 0.0
!PPL  roda_sfce = 0.0
!PPL  roda_sfc0 = 0.0
!PPL  roda_upae = 0.0
!PPL  roda_upa0 = 0.0
!PPL  roda_hgt  = 0.0
!PPL  roda_zfact = 0.0
!PPL  wt_cu_grid = 0.0
!PPL  lite_vars = " "
!PPL  itoptflg = 0
!PPL  isstflg = 0
!PPL  ivegtflg = 0
!PPL  isoilflg = 0
!PPL  ndviflg = 0
!PPL  nofilflg = 0
!PPL  itoptfn = " "
!PPL  isstfn = " "
!PPL  ivegtfn = " "
!PPL  isoilfn = " "
!PPL  ndvifn = " "
!PPL  itopsflg = 0
!PPL  toptenh = 0.0
!PPL  toptwvl = 0.0
!PPL  iz0flg = 0
!PPL  z0max = 0.0
!PPL  nnqparm = 0
!PPL  gnu = 0.0
!PPL  iplfld = " "
!PPL  ixsctn = 0
!PPL  isbval = 0
!PPL  us = 0.0
!PPL  vs = 0.0
!PPL  ts = 0.0
!PPL  ps = 0.0
!PPL  hs = 0.0
!PPL  rts = 0.0
!PPL  nnshcu = 0
!PPL  co2_init = 0.0
!PPL  slz = 0.0
!PPL  slmstr = 0.0
!PPL  stgoff = 0.0
!PPL  idiffk = 0
!PPL  csx = 0.0
!PPL  csz = 0.0
!PPL  xkhkm = 0.0
!PPL  zkhkm = 0.0
!PPL  akmin = 0.0
!PPL  levth = 0
!PPL  notid = " "
!PPL  gridwt = 0.0
!PPL  wvlnth = 0.0
!PPL  swvlnth = 0.0
!PPL  respon = 0.0
!PPL  domain_fname = ''


! PPL - begin defaults
  ! CATT_INFO
  CATT      = 0
  PLUMERISE = 0
  PRFRQ = 3600. ! Initial Value for PlumeRise Frequency 
  RECYCLE_TRACERS = 0
  CHEM_ASSIM = 0
  CHEMISTRY = 0
  CHEMISTRY_AQ = 0
  AEROSOL = 0
  CHEM_TIMESTEP=0.
  SRCMAPFN='NONE'
  SPLIT_METHOD=' '
  DEF_PROC_SRC= 'STOP'
  DIUR_CYCLE(1:NSRC)=1

  ! ISAN_CONTROL
  iszstage            = 1
  ivrstage            = 1
  isan_inc            = 0600
  guess1st	      = 'PRESS'
  i1st_flg	      = 1
  iupa_flg	      = 3
  isfc_flg	      = 3
  iapr		      = './dprep/dp' ! 2
  iarawi	      = ''
  iasrfce	      = ''
  varpfx	      = './ivar/iv-def' ! 2
  ioflgisz 	      = 0
  ioflgvar 	      = 1

  ! ISAN_ISENTROPIC
  nisn                = 43
  levth               = 800
  levth(1:nisn)       = (/280,282,284,286,288,290,292,294,296,298,300,303,306,309,&
       312,315,318,321,324,327,330,335,340,345,350,355,360,380,400,420,440, &
       460,480,500,520,540,570,600,630,670,700,750,800/)
  nigrids             = 1
  topsigz	      = 20000.
  hybbot	      = 4000.
  hybtop	      = 6000.
  sfcinf	      = 1000.
  sigzwt	      = 1.
  nfeedvar            = 1.
  maxsta 	      = 150
  maxsfc 	      = 1000
  notsta 	      = 0
  notid               = ''
  iobswin	      = 1800
  stasep	      = .1
  igridfl	      = 3
  gridwt	      = .01
  gobsep	      = 2.
  gobrad	      = 2.
  wvlnth	      = 1000.
  swvlnth	      = 500.
  respon	      = .90

  ! MODEL_FILE_INFO
  initial	      = 2 ! 2
  nud_type	      = 2 ! 2
  varfpfx	      = varpfx ! will be rewrited on the namelist read, and after too
  vwait1	      = 0.
  vwaittot	      = 0.
  nud_hfile	      = '' ! 2
  nudlat	      = 5 ! 2
  tnudlat	      = 3600. ! 2
  tnudcent	      = 43200. ! 2
  tnudtop	      = 10800. ! 2
  znudtop	      = 16000. ! 2
  wt_nudge_grid       = 1. ! 2
  wt_nudge_grid(1:4)  = (/1.,0.8,0.7,0.5/) ! 2
  wt_nudge_uv	      = 1.
  wt_nudge_th	      = 1.
  wt_nudge_pi	      = 1.
  wt_nudge_rt	      = 1.
  nud_cond	      = 0
  cond_hfile	      = ''
  tcond_beg	      = 0.
  tcond_end	      = 21600.
  t_nudge_rc	      = 3600.
  wt_nudgec_grid      = 0.5
  wt_nudgec_grid(1:4) = (/1.,0.8,0.7,0.5/)
  if_oda	      = 0
  oda_upaprefix       = ''
  oda_sfcprefix       = ''
  frqoda 	      = 300.
  todabeg	      = 0.
  todaend	      = 9999999.
  tnudoda	      = 900.
  wt_oda_grid         = 1.
  wt_oda_grid(1:4)    = (/1.,0.8,0.7,0.5/)
  wt_oda_uv	      = 1.
  wt_oda_th	      = 1.
  wt_oda_pi	      = 1.
  wt_oda_rt	      = 1.
  roda_sfce	      = 0.
  roda_sfce(1:4)      = (/50000.,100.,100.,100./)
  roda_sfc0           = 0.
  roda_sfc0(1:4)      = (/100000.,100000.,100000.,100000./)
  roda_upae           = 0.
  roda_upae(1:4)      = (/100000.,200.,200.,200./)
  roda_upa0           = 0.
  roda_upa0(1:4)      = (/200000.,2000.,2000.,2000./)
  roda_hgt            = 0.
  roda_hgt(1:4)       = (/3000.,3000.,3000.,3000./)
  roda_zfact          = 0.
  roda_zfact(1:4)     = (/100.,100.,100.,100./)
  oda_sfc_til 	      = 21600.
  oda_sfc_tel 	      = 900.
  oda_upa_til 	      = 43200.
  oda_upa_tel 	      = 21600.
  oda_upa_tel 	      = 21600.
  if_cuinv            = 0
  cu_prefix           = ''
  tnudcu              = 900.
  wt_cu_grid          = 1.
  wt_cu_grid(1:4)     = (/1.,1.,0.5,0.5/)
  tcu_beg	      = 0.
  tcu_end	      = 7200.
  cu_tel 	      = 3600.
  cu_til 	      = 21600.
  timstr 	      = 0
  hfilin 	      = ''
  ipastin	      = 0 ! 2
  pastfn 	      = '' ! 2
  ioutput	      = 2 ! 2
  hfilout	      = './H/hist-def' ! 2
  afilout	      = './A/anal-def' ! 2
  iclobber 	      = 1 ! 2
  ihistdel 	      = 0 ! 2
  frqhis	      = 21600. ! 2
  frqanl	      = 10800. ! 2
  frqlite             = 0.
  xlite 	      = '/0:0/'
  ylite 	      = '/0:0/'
  zlite 	      = '/0:0/'
  nlite_vars	      = 0
  lite_vars 	      = ''
  lite_vars(1:3)      = (/'UP','VP','WP'/)
  lite_vars(4)      = 'THETA'
  avgtim 	      = 0.
  frqmean	      = 0.
  frqboth	      = 0.
  kwrite 	      = 0
  frqprt 	      = 21600.
  initfld	      = 1
  topfiles	      = './data/toph-def'
  sfcfiles	      = './data/sfc-def'
  sstfpfx 	      = './data/sst-def'
  ndvifpfx	      = './data/ndvi-def'
  itoptflg	      = 2 ! 2
  itoptflg(1:4)       = (/2,2,2,2/) ! 2
  isstflg             = 2 ! 2
  isstflg(1:4)        = (/2,2,2,2/) ! 2
  ivegtflg            = 2 ! 2
  ivegtflg(1:4)       = (/2,2,2,2/) ! 2
  isoilflg            = 2 ! 2
  isoilflg(1:4)       = (/2,2,2,2/) ! 2
  ndviflg             = 2 ! 2
  ndviflg(1:4)        = (/2,2,2,2/) ! 2
  nofilflg            = 2
  nofilflg(1:4)       = (/2,2,2,2/)
  iupdndvi            = 0
  iupdsst	      = 0
  itoptfn	      = ''
  itoptfn(1)          = './topo10km/H'
  itoptfn(2:4)        = (/'./topo/EL','./topo/EL','./topo/EL'/)
  isstfn              = ''
  isstfn(1:4)         = (/'./sst/S','./sst/S','./sst/S','./sst/S'/)
  ivegtfn             = ''
  ivegtfn(1:4)        = (/'./veget/OGE','./veget/OGE','./veget/OGE','./veget/OGE'/)
  isoilfn             = ''
  isoilfn(1:4)        = (/'./soil-fao/FAO','./soil-fao/FAO','./soil-fao/FAO','./soil-fao/FAO'/)
  ndvifn              = ''
  ndvifn(1:4)         = (/'./ndvi-modis/N','./ndvi-modis/N','./ndvi-modis/N','./ndvi-modis/N'/)
  itopsflg            = 0
  itopsflg(1:4)       = (/1,1,1,0/)
  toptenh             = 0.
  toptenh(1:4)        = (/1.,1.,1.,0./)
  toptwvl             = 0.
  toptwvl(1:4)        = (/3.,2.,2.,0./)
  iz0flg              = 0
  iz0flg(1:4)         = (/0,0,0,0/)
  z0max               = 5.
  z0max(1:4)          = (/5.,5.,5.,5./)
  z0fact              = 0.005
  mkcoltab            = 0 ! duvida!
  coltabfn            = './micro/ct2.0' ! duvida!
  prtcputime          = 0

  ! MODEL_GRIDS
  expnme              = 'BRAMS 41' ! 2
  timeunit 	      = 'h' ! 2
  load_bal 	      = 0
  ngrids 	      = 1 ! 2
  nzg    	      = 9 ! 2
  nzs    	      = 4
  nxtnest	      = 1 ! 2
  nxtnest(1:4)        = (/0,1,2,3/) ! 2
  if_adap	      = 0 ! 2
  ihtran 	      = 1
  deltaz 	      = 120 ! 2
  dzrat  	      = 1.2 ! 2
  dzmax  	      = 1000.
  zz     	      = 19700.
  zz(1:41)            = (/0.0,20.0,46.0,80.0,120.0,165.0,220.0,290.0,380.0,480.0,590.0, &
       720.0,870.0,1030.0,1200.0,1380.0,1595.0,1850.0,2120.0,2410.0,2715.0,  &
       3030.0,3400.0,3840.0,4380.0,5020.0,5800.0,6730.0,7700.0,8700.0,9700.0,&
       10700.,11700.,12700.,13700.,14700.,15700.,16700.,17700.,18700.,19700./)
  dtlong  	      = 30. ! 2
  nacoust 	      = 3
  ideltat 	      = 1 ! 2
  nstratx 	      = 3
  nstratx(1:4)        = (/1,3,3,3/)
  nstraty             = 3
  nstraty(1:4)        = (/1,3,3,3/)
  nndtrat             = 3
  nndtrat(1:4)        = (/1,3,3,3/)
  nestz1              = 0
  nstratz1            = 1
  nstratz1(1:4)       = (/2,2,2,1/)
  nestz2              = 0
  nstratz2            = 1
  nstratz2(1:4)       = (/3,3,2,1/)
  ninest              = 0 ! 2
  ninest(1:4)         = (/1,0,0,0/) ! 2
  njnest              = 0 ! 2
  njnest(1:4)         = (/1,0,0,0/) ! 2
  nknest              = 1
  nknest(1:4)         = (/1,1,1,1/)
  nnsttop             = 1
  nnsttop(1:4)        = (/1,1,1,1/)
  nnstbot             = 1
  nnstbot(1:4)        = (/1,1,1,1/)
  gridu               = 0.
  gridu(1:4)          = (/0.,0.,0.,0./)

!--(DMK-CCATT)---------------------------------------------------------
! STDLAT1 = (default)
! STDLAT2 = (default)
!--(DMK-CCATT-END)-----------------------------------------------------

  gridv               = 0.
  gridv(1:4)          = (/0.,0.,0.,0./)
  domain_fname        = 'S'

  ! MODEL_OPTIONS
  naddsc  	      = 4
  icorflg 	      = 1

!--(DMK-CCATT)---------------------------------------------------------
  !ml/srf for stilt/mass cons
  iexev               = 1
  imassflx            = 0
!--(DMK-CCATT-END)-----------------------------------------------------

  ibnd    	      = 1
  jbnd    	      = 1
  cphas   	      = 20.
  lsflg   	      = 0
  nfpt    	      = 0
  distim  	      = 400.
  iswrtyp 	      = 1
  ilwrtyp 	      = 1
  raddatfn	      = "./carma/rad_param.data" ! 2
  radfrq  	      = 900. !2
  lonrad  	      = 1
  nnqparm 	      = 2
  closure_type        = "EN" ! 2
  nnshcu      	      = 1 ! 2
  nnshcu(1:4) 	      = (/1,1,1,1/) !2
  confrq      	      = 1200. ! 2
  shcufrq     	      = 1200. ! 2
  wcldbs      	      = .0005
  npatch      	      = 2 ! 2
  nvegpat     	      = 1
  n_co2       	      = 1
  co2_init    	      = 340.
  co2_init(1:9)       = (/360.,360.,360.,355.,355.,355.,350.,350.,340./)
  isfcl               = 1 ! 2
  nvgcon              = 6 ! 2
  pctlcon             = 1.
  nslcon 	      = 6 ! 2
  zrough 	      = .05
  albedo 	      = .2
  seatmp 	      = 298. ! 2
  dthcon 	      = 0.
  drtcon 	      = 0.
  soil_moist          = "i" ! 2
  soil_moist_fail     = "l" ! 2
  usdata_in  	      = "./soil-moisture/GL_SM.GPNR." ! 2
  usmodel_in 	      = "./data/SM-" ! 2
  slz        	      = -0.1 ! 2
  slz(1:9)   	      = (/-2.0,-1.75,-1.50,-1.25,-1.00,-0.75,-0.50,-0.25,-0.1/)
  slmstr     	      = 0.28 ! 2
  slmstr(1:9)	      = (/0.40,0.37,0.35,0.33,0.32,0.31,0.30,0.29,0.28/) ! 2
  stgoff     	      = 0.0
  stgoff(1:9)	      = (/0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0,0.0/)
  if_urban_canopy     = 0
  idiffk              = 1 ! 2
  idiffk(1:4)         = (/1,1,1,1/) ! 2
  ihorgrad            = 2
  csx                 = .2
  csx(1:4) 	      = (/.2,.2,.2,.2/)
  csz      	      = .2
  csz(1:4) 	      = (/.35,.35,.35,.35/)
  xkhkm    	      = 3.
  xkhkm(1:4) 	      = (/3.,3.,3.,3./)
  zkhkm      	      = 3.
  zkhkm(1:4) 	      = (/3.,3.,3.,3./)
  akmin      	      = 1.
  akmin(1:4) 	      = (/1.,1.,1.,1./)
  level  	      = 3 ! 2
  icloud 	      = 4 ! 2
  irain  	      = 2 ! 2
  ipris  	      = 5
  isnow  	      = 2 ! 2
  iaggr  	      = 2 ! 2
  igraup 	      = 2 ! 2
  ihail  	      = 2 ! 2
  cparm  	      = .1e9
  rparm  	      = 1e-3
  pparm  	      = 0.
  sparm  	      = 1e-3
  aparm  	      = 1e-3
  gparm  	      = 1e-3
  hparm  	      = 3e-3
  gnu    	      = 2.
  gnu(1:4)	      = (/2.,2.,2.,2./)

  ! MODEL_PRINT
  nplt   	      = 0
  iplfld 	      = ' '
  ixsctn 	      = 3
  ixsctn(1:4)         = (/3,3,3,3/)
  isbval              = 2
  isbval(1:4)         = (/2,2,2,2/)

  ! MODEL_SOUND
  ipsflg  	      = 1 ! 2
  itsflg  	      = 0 ! 2
  irtsflg 	      = 3 ! 2
  iusflg  	      = 0 ! 2
  hs      	      = 0. ! 2
  ps(1:11)	      = (/1010.,1000.,2000.,3000.,4000.,6000.,8000.,11000.,15000.,20000.,25000./) ! 2
  ts(1:11)	      = (/25.,18.5,12.,4.5,-11.,-24.,-37.,-56.5,-56.5,-56.5,-56.5/) ! 2
  rts(1:11) 	      = (/70.,70.,70.,70.,20.,20.,20.,20.,10.,10.,10./) ! 2
  us(1:11)  	      = (/10.,10.,10.,10.,10.,10.,10.,10.,10.,10.,10./) ! 2
  vs(1:11)  	      = (/0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0./) ! 2

  ! TEB
  teb_spm  	      = 0
  fusfiles 	      = ''
  ifusflg  	      = 0
  ifusflg(1:4)        = (/1,1,1,1/)
  ifusfn 	      = ''
  ifusfn(1:4)	      = (/'./fusos/fuso','./fusos/fuso','./fusos/fuso','./fusos/fuso'/) ! 2
  ichemi     	      = 1    !Photochemical module activation - 1=on, 0=off
  ichemi_in  	      = 0    !Use initial values from previous run (1=yes,0=no) ! 2
  chemdata_in	      = ''
  isource    	      = 1    !Emission module activation - 1=on, 0=off ! 2
  weekdayin  	      = 'SUN'  !Initial weeakday of the simulation
  rushh1     	      = 7.81  !Morning Rush Hour (Local Time in Hours)
  rushh2     	      = 16.0  !Afternoon/Evening Rush Hour (Local Time)
  daylight   	      = 0.    !Daylight saving time (horario de verao)
  ! Emission factor (fraction of weekdays) for Saturdays and Sundays
  ! They are used in the emission module and TEB. - EDF
  efsat     	      = 0.8
  efsun     	      = 0.5
  ! Input GMT difference time variable (To define local time)
  !Industrial emissions (kg/s/m2)
  eindno     	      = 2.6636227e-10
  eindno2    	      = 2.9595805e-11
  eindpm     	      = 4.3421278e-10
  eindco     	      = 8.1599860e-10
  eindso2    	      = 3.6149164e-10
  eindvoc    	      = 2.5367833e-10 
  !Veicular emissions (kg/day/m2)
  eveino     	      = 4.3196708e-04
  eveino2    	      = 6.8566209e-05
  eveipm     	      = 6.2648396e-06
  eveico     	      = 7.5433785e-03
  eveiso2    	      = 4.0730592e-05
  eveivoc    	      = 1.1892237e-03
  !----- Urban canopy parameterization using TEB (Masson, 2000)-------------
  iteb      	      = 1     !1=on, 0=off
  tminbld   	      = 12.   !Minimum internal building temperature (degrees Celsius)
  nteb      	      = 3     !Number of roof,road and wall layers used in TEB, Max.3
  ! ROOF layers properties
  ! heat capacity
  hc_roof             = 0.
  hc_roof(1:3)        = (/2110000.,280000.,290000./)
  ! thermal conductivity
  tc_roof             = 0.
  tc_roof(1:3)        = (/0.41,0.05,0.03/)
  ! depth
  d_roof              = 0.
  d_roof(1:3)         = (/0.05,0.4,0.05/)
  ! ROAD layers properties
  ! heat capacity
  hc_road             = 0.
  hc_road(1:3)        = (/1240000.,1280000.,1280000./)
  ! thermal conductivity 1.01
  tc_road             = 1.0103
  ! depth
  d_road              = 0.
  d_road(1:3)         = (/0.05,0.1,1.0/)
  ! WALL layers properties
  ! heat capacity J/m3/K 10e6
  hc_wall             = 1000000.
  ! thermal conductivity 0.81 W/m/K
  tc_wall             = 0.81
  ! depth
  d_wall              = 0.
  d_wall(1:3)         = (/0.02,0.125,0.02/)
  nurbtype            = 2	  !Number of urban types (maximum of 3)

  !Leaf class code to identify each urban type
  ileafcod            = 19
  ileafcod(1:2)       = (21,19)
  !Urban type properties
  !Urban type roughness length 5 e 1
  z0_town             = 0.0
  z0_town(1:2)        = (/3.0,0.5/)
  !Fraction occupied by buildings in the grid cell
  bld      	      = 0.0
  bld(1:2) 	      = (/0.5,0.7/)
  !Building Height
  bld_height          = 0
  bld_height(1:2)     = (/50.,5.0/)
  !Vertical/Horizontal rate 3 e 0.5
  bld_hl_ratio        = 2.4
  bld_hl_ratio(1:2)   = (/4.4,2.4/)
  !Roof albedo
  aroof               = 0.15
  !Roof emissivitiy
  eroof               = 0.9
  !Road albedo
  aroad               = 0.1
  !Road emissivity 90% masson
  eroad               = 0.9
  !Wall albedo
  awall               = 0.25
  !Wall emissivity
  ewall               = 0.85
  !Maximum value of sensible heat
  htraf      	      = 0.
  htraf(1:2) 	      = (/90.0,60.0/)
  !Maximum value of sensible heat
  hindu      	      = 14.
  hindu(1:2) 	      = (/10.0,14.0/)
  !released by Industry (W/m2)
  !Maximum value of latent heat
  pletraf             = 5.
  pletraf(1:2)        = (/10.0,5.0/)
  !released by Traffic (W/m2)
  !Maximum value of latent heat
  pleindu             = 50.
  pleindu(1:2)        = (/30.0,50.0/)

  !PPL defaults for necessary variables
  ! (values from the first-time users)
  ! ATENTION: those variables below should be declared on the simplest RAMSIN
  runtype 	      = "initial"
  timmax  	      = 24
  imonth1 	      = 01
  idate1  	      = 25
  iyear1  	      = 2005
  itime1  	      = 0000
  nnxp    	      = 35
  nnyp    	      = 34
  nnzp    	      = 32
  deltax  	      = 112000.
  deltay  	      = 112000.
  polelat 	      = -23.
  polelon 	      = -52.5
  centlat 	      = -22.
  centlon             = -56.

  ! PPL end defaults

  ! select unused i/o unit

  do iunit = firstUnit, lastUnit
     inquire(iunit,opened=op)
     if (.not. op) exit
  end do

  if (iunit > lastUnit) then
     call fatal_error(h//" all i/o units in use")
  end if

  ! if namelist file exists, open, read each section and close

  inquire(file=trim(fileName), exist=ex)
  if (.not. ex) then
     call fatal_error(h//" namelist file "//trim(fileName)//&
          " does not exist")
  end if

  open(iunit, file=trim(fileName), status="old", action="read",&
       iostat=err)
  if (err /= 0) then
     write(c0,"(i10)") err
     call fatal_error(h//" open namelist file "//trim(fileName)//&
          " returned iostat="//trim(adjustl(c0)))
  end if

  read (iunit, iostat=err, NML=MODEL_GRIDS)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section MODEL_GRIDS "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write(*,*) "expnme=",trim(expnme)
     write(*,*) "runtype=",trim(runtype)
     write(*,*) "timeunit=",trim(timeunit)
     write(*,*) "timmax=",timmax
     write(*,*) "load_bal=",load_bal
     write(*,*) "imonth1=",imonth1
     write(*,*) "idate1=",idate1
     write(*,*) "iyear1=",iyear1
     write(*,*) "itime1=",itime1
     write(*,*) "ngrids=",ngrids
     write(*,*) "nnxp=",nnxp
     write(*,*) "nnyp=",nnyp
     write(*,*) "nnzp=",nnzp
     write(*,*) "nzg=",nzg
     write(*,*) "nzs=",nzs
     write(*,*) "nxtnest=",nxtnest
     write(*,*) "DOMAIN_FNAME=", DOMAIN_FNAME
     write(*,*) "if_adap=",if_adap
     write(*,*) "ihtran=",ihtran
     write(*,*) "deltax=",deltax
     write(*,*) "deltay=",deltay
     write(*,*) "deltaz=",deltaz
     write(*,*) "dzrat=",dzrat
     write(*,*) "dzmax=",dzmax
     write(*,*) "zz=",zz
     write(*,*) "dtlong=",dtlong
     write(*,*) "nacoust=",nacoust
     write(*,*) "ideltat=",ideltat
     write(*,*) "nstratx=",nstratx
     write(*,*) "nstraty=",nstraty
     write(*,*) "nndtrat=",nndtrat
     write(*,*) "nestz1=",nestz1
     write(*,*) "nstratz1=",nstratz1
     write(*,*) "nestz2=",nestz2
     write(*,*) "nstratz2=",nstratz2
     write(*,*) "polelat=",polelat
     write(*,*) "polelon=",polelon
     write(*,*) "centlat=",centlat
     write(*,*) "centlon=",centlon
     write(*,*) "ninest=",ninest
     write(*,*) "njnest=",njnest
     write(*,*) "nknest=",nknest
     write(*,*) "nnsttop=",nnsttop
     write(*,*) "nnstbot=",nnstbot
     write(*,*) "gridu=",gridu
     write(*,*) "gridv=",gridv

!--(DMK-CCATT)---------------------------------------------------------
     write(*,*) "STDLAT1=",STDLAT1
     write(*,*) "STDLAT2=",STDLAT2
!--(DMK-CCATT-END)-----------------------------------------------------

     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=CATT_INFO)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section CATT_INFO "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     print *, "CATT=", CATT

!--(DMK-CCATT)---------------------------------------------------------
     write(*,*) "SRCMAPFN=", SRCMAPFN
     write(*,*) "DEF_PROC_SRC=", def_proc_src
     write(*,*) "DIUR_CYCLE=", diur_cycle
     write(*,*) "RADDATFN=", RADDATFN
     write(*,*) "NA_EXTRA2D=", NA_EXTRA2D
     write(*,*) "NA_EXTRA3D=", NA_EXTRA3D
     write(*,*) "RECYCLE_TRACERS=", RECYCLE_TRACERS
     write(*,*) "CHEM_ASSIM=", CHEM_ASSIM
     write(*,*) "CHEMISTRY=", CHEMISTRY
     write(*,*) "CHEMISTRY_AQ=", CHEMISTRY_AQ
     write(*,*) "AEROSOL=", AEROSOL
     write(*,*) "SPLIT_METHOD=", SPLIT_METHOD
     write(*,*) "CHEM_TIMESTEP=", CHEM_TIMESTEP
     write(*,*) "PLUMERISE=", PLUMERISE
     write(*,*) "PRFRQ=", PRFRQ
!--(DMK-original)------------------------------------------------------
!     write(*,*) "FIREMAPFN=", FIREMAPFN
!--(DMK-CCATT-END)-----------------------------------------------------

     !!write(*,*) "TRACERSFN=", TRACERSFN
     write(*,*) "PLUMERISE=", PLUMERISE
     write(*,*) "PRFRQ=", PRFRQ
     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=TEB_SPM_INFO)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section TEB_SPM_INFO "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     print *, "TEB_SPM=", TEB_SPM
     print *, "ifusflg=", ifusflg
     print *, "ifusfn=", ifusfn
     print *, "fusfiles=", trim(fusfiles)
     print *, "ICHEMI=", ICHEMI
     print *, "ICHEMI_IN=", ICHEMI_IN
     print *, "CHEMDATA_IN=", CHEMDATA_IN
     print *, "ISOURCE=", ISOURCE
     print *, "WEEKDAYIN=", trim(WEEKDAYIN)
     print *, "RUSHH1=", RUSHH1
     print *, "RUSHH2=", RUSHH2
     print *, "DAYLIGHT=", DAYLIGHT
     print *, "EFSAT=", EFSAT
     print *, "EFSUN=", EFSUN
     print *, "EINDNO=", EINDNO
     print *, "EINDNO2=", EINDNO2
     print *, "EINDPM=", EINDPM
     print *, "EINDCO=", EINDCO
     print *, "EINDSO2=", EINDSO2
     print *, "EINDVOC=", EINDVOC
     print *, "EVEINO=", EVEINO
     print *, "EVEINO2=", EVEINO2
     print *, "EVEIPM=", EVEIPM
     print *, "EVEICO=", EVEICO
     print *, "EVEISO2=", EVEISO2
     print *, "EVEIVOC=", EVEIVOC
     print *, "ITEB=", ITEB
     print *, "TMINBLD=", TMINBLD
     print *, "NTEB=", NTEB
     print *, "HC_ROOF=", HC_ROOF
     print *, "TC_ROOF=", TC_ROOF
     print *, "D_ROOF=", D_ROOF
     print *, "HC_ROAD=", HC_ROAD
     print *, "TC_ROAD=", TC_ROAD
     print *, "D_ROAD=", D_ROAD
     print *, "HC_WALL=", HC_WALL
     print *, "TC_WALL=", TC_WALL
     print *, "D_WALL=", D_WALL
     print *, "NURBTYPE=", NURBTYPE
     print *, "ILEAFCOD=", ILEAFCOD
     print *, "Z0_TOWN=", Z0_TOWN
     print *, "BLD=", BLD
     print *, "BLD_HEIGHT=", BLD_HEIGHT
     print *, "BLD_HL_RATIO=", BLD_HL_RATIO
     print *, "AROOF=", AROOF
     print *, "EROOF=", EROOF
     print *, "AROAD=", AROAD
     print *, "EROAD=", EROAD
     print *, "AWALL=", AWALL
     print *, "EWALL=", EWALL
     print *, "HTRAF=", HTRAF
     print *, "HINDU=", HINDU
     print *, "PLETRAF=", PLETRAF
     print *, "PLEINDU=", PLEINDU
     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=MODEL_FILE_INFO)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section MODEL_FILE_INFO "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write (*, "(a)") " namelist MODEL_FILE_INFO: "
     write (*,*) "initial=", initial
     write (*,*) "nud_type=", nud_type
     write (*,*) "varfpfx=", trim(varfpfx)
     write (*,*) "vwait1=", vwait1
     write (*,*) "vwaittot=", vwaittot
     write (*,*) "nud_hfile=", trim(nud_hfile)
     write (*,*) "nudlat=", nudlat
     write (*,*) "tnudlat=", tnudlat
     write (*,*) "tnudcent=", tnudcent
     write (*,*) "tnudtop=", tnudtop
     write (*,*) "znudtop=", znudtop
     write (*,*) "wt_nudge_grid=", wt_nudge_grid
     write (*,*) "wt_nudge_uv=", wt_nudge_uv
     write (*,*) "wt_nudge_th=", wt_nudge_th
     write (*,*) "wt_nudge_pi=", wt_nudge_pi
     write (*,*) "wt_nudge_rt=", wt_nudge_rt
     write (*,*) "nud_cond=", nud_cond
     write (*,*) "cond_hfile=", trim(cond_hfile)
     write (*,*) "tcond_beg=", tcond_beg
     write (*,*) "tcond_end=", tcond_end
     write (*,*) "t_nudge_rc=", t_nudge_rc
     write (*,*) "wt_nudgec_grid=", wt_nudgec_grid
     write (*,*) "if_oda=", if_oda
     write (*,*) "oda_upaprefix=", trim(oda_upaprefix)
     write (*,*) "oda_sfcprefix=", trim(oda_sfcprefix)
     write (*,*) "frqoda=", frqoda
     write (*,*) "todabeg=", todabeg
     write (*,*) "todaend=", todaend
     write (*,*) "tnudoda=", tnudoda
     write (*,*) "wt_oda_grid=", wt_oda_grid
     write (*,*) "wt_oda_uv=", wt_oda_uv
     write (*,*) "wt_oda_th=", wt_oda_th
     write (*,*) "wt_oda_pi=", wt_oda_pi
     write (*,*) "wt_oda_rt=", wt_oda_rt
     write (*,*) "roda_sfce=", roda_sfce
     write (*,*) "roda_sfc0=", roda_sfc0
     write (*,*) "roda_upae=", roda_upae
     write (*,*) "roda_upa0=", roda_upa0
     write (*,*) "roda_hgt=", roda_hgt
     write (*,*) "roda_zfact=", roda_zfact
     write (*,*) "oda_sfc_til=", oda_sfc_til
     write (*,*) "oda_sfc_tel=", oda_sfc_tel
     write (*,*) "oda_upa_til=", oda_upa_til
     write (*,*) "oda_upa_tel=", oda_upa_tel
     write (*,*) "if_cuinv=", if_cuinv
     write (*,*) "cu_prefix=", trim(cu_prefix)
     write (*,*) "tnudcu=", tnudcu
     write (*,*) "wt_cu_grid=", wt_cu_grid
     write (*,*) "tcu_beg=", tcu_beg
     write (*,*) "tcu_end=", tcu_end
     write (*,*) "cu_tel=", cu_tel
     write (*,*) "cu_til=", cu_til
     write (*,*) "timstr=", timstr
     write (*,*) "hfilin=", trim(hfilin)
     write (*,*) "ipastin=", ipastin
     write (*,*) "pastfn=", trim(pastfn)
     write (*,*) "ioutput=", ioutput
     write (*,*) "hfilout=", trim(hfilout)
     write (*,*) "afilout=", trim(afilout)
     write (*,*) "iclobber=", iclobber
     write (*,*) "ihistdel=", ihistdel
     write (*,*) "frqhis=", frqhis
     write (*,*) "frqanl=", frqanl
     write (*,*) "frqlite=", frqlite
     write (*,*) "xlite=", xlite
     write (*,*) "ylite=", ylite
     write (*,*) "zlite=", zlite
     write (*,*) "nlite_vars=", nlite_vars
     write (*,*) "lite_vars=", (trim(lite_vars(i))//";", i=1,size(lite_vars))
     write (*,*) "avgtim=", avgtim
     write (*,*) "frqmean=", frqmean
     write (*,*) "frqboth=", frqboth
     write (*,*) "kwrite=", kwrite
     write (*,*) "frqprt=", frqprt
     write (*,*) "initfld=", initfld
     write (*,*) "prtcputime", prtcputime
     write (*,*) "topfiles=", trim(topfiles)
     write (*,*) "sfcfiles=", trim(sfcfiles)
     write (*,*) "sstfpfx=", trim(sstfpfx)
     write (*,*) "ndvifpfx=", trim(ndvifpfx)
     write (*,*) "itoptflg=", itoptflg
     write (*,*) "isstflg=", isstflg
     write (*,*) "ivegtflg=", ivegtflg
     write (*,*) "isoilflg=", isoilflg
     write (*,*) "ndviflg=", ndviflg
     write (*,*) "nofilflg=", nofilflg
     write (*,*) "iupdndvi=", iupdndvi
     write (*,*) "iupdsst=", iupdsst
     write (*,*) "itoptfn=", (trim(itoptfn(i))//";", i =1,size(itoptfn))
     write (*,*) "isstfn=", (trim(isstfn(i))//";", i=1,size(isstfn))
     write (*,*) "ivegtfn=", (trim(ivegtfn(i))//";", i = 1, size(ivegtfn))
     write (*,*) "isoilfn=", (trim(isoilfn(i))//";", i = 1, size(isoilfn))
     write (*,*) "ndvifn=", (trim(ndvifn(i))//";", i=1,size(ndvifn))
     write (*,*) "itopsflg=", itopsflg
     write (*,*) "toptenh=", toptenh
     write (*,*) "toptwvl=", toptwvl
     write (*,*) "iz0flg=", iz0flg
     write (*,*) "z0max=", z0max
     write (*,*) "z0fact=", z0fact
     write (*,*) "mkcoltab=", mkcoltab
     write (*,*) "coltabfn=", trim(coltabfn)
     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=MODEL_OPTIONS)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section MODEL_OPTIONS "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write (*,*) "naddsc=",naddsc
     write (*, *) "icorflg=",icorflg
     write (*, *) "ibnd=",ibnd
     write (*, *) "jbnd=",jbnd
     write (*, *) "cphas=",cphas
     write (*, *) "lsflg=",lsflg
     write (*, *) "nfpt=",nfpt
     write (*, *) "distim=",distim
     write (*, *) "iswrtyp=",iswrtyp
     write (*, *) "ilwrtyp=",ilwrtyp
     write (*, *) "raddatfn=", RADDATFN
     write (*, *) "radfrq=",radfrq
     write (*, *) "lonrad=",lonrad
     write (*, *) "nnqparm=",nnqparm
     write (*, *) "closure_type=",closure_type
     write (*, *) "nnshcu=",nnshcu
     write (*, *) "confrq=",confrq
     write (*, *) "shcufrq=",shcufrq
     write (*, *) "wcldbs=",wcldbs
     write (*, *) "npatch=",npatch
     write (*, *) "nvegpat=",nvegpat
     write (*, *) "isfcl=",isfcl
     write (*, *) "n_co2=",n_co2
     write (*, *) "co2_init=",co2_init
     write (*, *) "nvgcon=",nvgcon
     write (*, *) "pctlcon=",pctlcon
     write (*, *) "nslcon=",nslcon
     write (*, *) "drtcon=",drtcon
     write (*, *) "zrough=",zrough
     write (*, *) "albedo=",albedo
     write (*, *) "seatmp=",seatmp
     write (*, *) "dthcon=",dthcon
     write (*, *) "soil_moist=",soil_moist
     write (*, *) "soil_moist_fail=",soil_moist_fail
     write (*, *) "usdata_in=",trim(usdata_in)
     write (*, *) "usmodel_in=",trim(usmodel_in)
     write (*, *) "slz=",slz
     write (*, *) "slmstr=",slmstr
     write (*, *) "stgoff=",stgoff
     write (*, *) "if_urban_canopy=",if_urban_canopy
     write (*, *) "idiffk=",idiffk
     write (*, *) "ihorgrad=",ihorgrad
     write (*, *) "csx=",csx
     write (*, *) "csz=",csz
     write (*, *) "xkhkm=",xkhkm
     write (*, *) "zkhkm=",zkhkm
     write (*, *) "akmin=",akmin
     write (*, *) "level=",level
     write (*, *) "icloud=",icloud
     write (*, *) "irain=",irain
     write (*, *) "ipris=",ipris
     write (*, *) "isnow=",isnow
     write (*, *) "iaggr=",iaggr
     write (*, *) "igraup=",igraup
     write (*, *) "ihail=",ihail
     write (*, *) "cparm=",cparm
     write (*, *) "rparm=",rparm
     write (*, *) "pparm=",pparm
     write (*, *) "sparm=",sparm
     write (*, *) "aparm=",aparm
     write (*, *) "gparm=",gparm
     write (*, *) "hparm=",hparm
     write (*, *) "gnu=",gnu

!--(DMK-CCATT)-----------------------------------------------------
     !ml/srf for mass conser/stilt     
     write (*, *) "iexev=", iexev
     write (*, *) "imassflx=", imassflx
!--(DMK-CCATT-END)-------------------------------------------------

     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=MODEL_SOUND)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section MODEL_SOUND "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write (*, *) "ipsflg=",ipsflg
     write (*, *) "itsflg=",itsflg
     write (*, *) "irtsflg=",irtsflg
     write (*, *) "iusflg=",iusflg
     write (*, *) "hs=",hs
     write (*, *) "ps=",ps
     write (*, *) "ts=",ts
     write (*, *) "rts=",rts
     write (*, *) "us=",us
     write (*, *) "vs=",vs
     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=MODEL_PRINT)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section MODEL_PRINT "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write (*, *) "nplt=",nplt
     write (*, *) "iplfld=",(trim(iplfld(i))//";", i=1,size(iplfld))
     write (*, *) "ixsctn=",ixsctn
     write (*, *) "isbval=",isbval
     call fatal_error(h//" reading namelist")
  end if


  read (iunit, iostat=err, NML=ISAN_CONTROL)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section ISAN_CONTROL "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write (*, *) "iszstage=",iszstage
     write (*, *) "ivrstage=",ivrstage
     write (*, *) "isan_inc=",isan_inc
     write (*, *) "guess1st=",guess1st
     write (*, *) "i1st_flg=",i1st_flg
     write (*, *) "iupa_flg=",iupa_flg
     write (*, *) "isfc_flg=",isfc_flg
     write (*, *) "iapr=",trim(iapr)
     write (*, *) "iarawi=",trim(iarawi)
     write (*, *) "iasrfce=",trim(iasrfce)
     write (*, *) "varpfx=",trim(varpfx)
     write (*, *) "ioflgisz=",ioflgisz
     write (*, *) "ioflgvar=",ioflgvar
     call fatal_error(h//" reading namelist")
  end if

  read (iunit, iostat=err, NML=ISAN_ISENTROPIC)
  if (err /= 0) then
     write(*,"(a)") h//"**(ERROR)** reading section ISAN_ISENTROPIC "//&
          &"of namelist file "//trim(fileName)
     write(*,"(a)") h//" compare values read with file contents:"
     write (*, *) "nisn=",nisn
     write (*, *) "levth=",levth
     write (*, *) "nigrids=",nigrids
     write (*, *) "topsigz=",topsigz
     write (*, *) "hybbot=",hybbot
     write (*, *) "hybtop=",hybtop
     write (*, *) "sfcinf=",sfcinf
     write (*, *) "sigzwt=",sigzwt
     write (*, *) "nfeedvar=",nfeedvar
     write (*, *) "maxsta=",maxsta
     write (*, *) "maxsfc=",maxsfc
     write (*, *) "notsta=",notsta
     write (*, *) "notid=",(trim(notid(i))//";", i=1,size(notid))
     write (*, *) "iobswin=",iobswin
     write (*, *) "stasep=",stasep
     write (*, *) "igridfl=",igridfl
     write (*, *) "gridwt=",gridwt
     write (*, *) "gobsep=",gobsep
     write (*, *) "gobrad=",gobrad
     write (*, *) "wvlnth=",wvlnth
     write (*, *) "swvlnth=",swvlnth
     write (*, *) "respon=",respon
     call fatal_error(h//" reading namelist")
  end if

  close(iunit, iostat=err)
  if (err /= 0) then
     write(c0,"(i10)") err
     call fatal_error(h//" closing file "//&
          trim(fileName)//" returned iostat="//&
          trim(adjustl(c0)))
  end if

  ! PPL - VARFPFX is the same variable of VARPFX
  VARFPFX = VARPFX


end subroutine ReadNamelist
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################
!###########################################################################

subroutine geodat_var(n2,n3,datr,hfn,ofn,vt2da,vt2db,ngr,vnam)

use mem_grid
use io_params

implicit none
integer :: n2,n3,ngr
real ::  vt2da(*),vt2db(*),datr(n2,n3)
character(len=80) :: hfn,ofn,title
character(len=3) :: vnam

integer :: lb,iblksizo,no,isbego,iwbego,iodim,mof,niq,njq,np,i,j
real :: offlat,offlon,deltallo,deltaxq,deltayq,deltaxp,deltayp,erad

real,allocatable:: dato(:)

!-srf
real TOPTWVL_2d(n2,n3)


print*,'====================================================='
if(VNAM(1:2).eq.'TO') then
   print*,'starting topography on grid:',NGR
elseif(VNAM(1:2).eq.'ZO') then
   print*,'starting surface roughness on grid:',NGR
else
   print*,'starting '//vnam//' data on grid:',NGR
endif

LB=len_trim(HFN)
if(LB.le.0) then
   print*,'==================================================='
   print*,'|  Problem in GEODAT, Input data prefix incorrect !'
   print*,'|  Grid :',ngrid
   print*,'|  File prefix:',HFN
   print*,'==================================================='
   stop 'GEODAT-file'
endif

if((vnam(1:2).eq.'TO'.or.vnam(1:2).eq.'ZO').and.  &
   (ITOPSFLG(NGR).eq.1.and.TOPTENH(NGR).gt.1.)) then
   print*,'==================================================='
   print*,'|  Problem in GEODAT, Silhouette weight too high !'
   print*,'|  Grid :',NGR
   print*,'|  Weight (range TOPTENH=0-1):',TOPTENH(NGR)
   print*,'==================================================='
   stop 'GEODAT'
endif

!     configure grid specs for raw data to rams grid (R) transfer

!     raw data grid (O)
if(vnam.eq.'TOD'.or.vnam.eq.'ZOD') then  !using dted data
   iblksizo=5 ! 5 degree squares
   no=600     ! 30" data over a 5 degree square
   isbego=-90
   iwbego=0
   offlat=0.
   offlon=0.
   DELTALLO=FLOAT(IBLKSIZO)/FLOAT(NO)
else
   TITLE=HFN(1:LB)//'HEADER'
   LB=len_trim(TITLE)
   call rams_f_open(29,title(1:lb),'FORMATTED','OLD','READ',0)
   READ(29,*)IBLKSIZO,NO,ISBEGO,IWBEGO,offlat,offlon
   CLOSE(29)
   DELTALLO=FLOAT(IBLKSIZO)/FLOAT(NO-1)
endif

iodim=max(100000,4*no*no)
MOF=IODIM/(NO*NO)

   allocate(dato(iodim+mof+mof))


!-srf:  get TOPTWVL with grid dependence
   call get_TOPTWVL(ngr,n2,n3,TOPTWVL_2d)
!-srf end

do j=2,n3-1;  do i=2,n2-1

!print*,'ij-----------------------------------------------',i,j;call flush(6)
!     temp grid (Q) - smoothing only applied to topo
if(vnam(1:2).eq.'TO') then
   
   !-srf : orig with TOPTWVL cte qq i,j
   !DELTAXQ=0.5*TOPTWVL(NGR) * ( xmn(i,ngr)-xmn(i-1,ngr) )!*DELTAXN(NGR)
   !DELTAYQ=0.5*TOPTWVL(NGR) * ( ymn(j,ngr)-ymn(j-1,ngr) ) 

   !-srf : TOPTWVL f(i,j)
   DELTAXQ=0.5*TOPTWVL_2d(i,j) * ( xmn(i,ngr)-xmn(i-1,ngr) )!*DELTAXN(NGR)
   DELTAYQ=0.5*TOPTWVL_2d(i,j) * ( ymn(j,ngr)-ymn(j-1,ngr) ) 
   !print*,'geodat1=',TOPTWVL_2d(i,j), DELTAYQ
 
else
!srf -orig
   !DELTAXQ=DELTAXN(NGR)
   !DELTAYQ=DELTAYN(NGR)
   DELTAXQ=( xmn(i,ngr)-xmn(i-1,ngr) )
   DELTAYQ=( ymn(j,ngr)-ymn(j-1,ngr) ) 
endif

!-versao1
!NIQ=INT(FLOAT(NNXP(NGR)-1)*( xmn(i,ngr)-xmn(i-1,ngr) )/DELTAXQ)+4
!NJQ=INT(FLOAT(NNYP(NGR)-1)*( ymn(j,ngr)-ymn(j-1,ngr) )/DELTAYQ)+4
!-versao opt
 NIQ=INT(( xmn(i,ngr)-xmn(i-1,ngr) )/DELTAXQ)+4
 NJQ=INT(( ymn(j,ngr)-ymn(j-1,ngr) )/DELTAYQ)+4

!     interpollated raw data grid (P)
NP=MIN(10,MAX(1,INT(DELTAXQ/(DELTALLO*111000.))))
DELTAXP=DELTAXQ/FLOAT(NP)
DELTAYP=DELTAYQ/FLOAT(NP)

CALL SFCOPQR_VAR(NO,MOF,NP,NIQ,NJQ,N2,N3,XTN(1,NGR),YTN(1,NGR)  &
     ,platn(ngr),plonn(ngr)  &
     ,ERAD,DELTALLO,DELTAXP,DELTAYP,DELTAXQ,DELTAYQ,IBLKSIZO  &
     ,ISBEGO,IWBEGO,DATO(1),VT2DA,VT2DB,DATR  &
     ,OFN,offlat,offlon,VNAM,NGR,itopsflg(ngr),iz0flg(ngr),i,j)

enddo;enddo

deallocate(dato)

!srf-telesc (cheque se esta correto)
 DATR(1,:) = DATR(2,:) ;  DATR(N2,:) = DATR(N2-1,:)
 DATR(:,1) = DATR(:,2) ;  DATR(:,N3) = DATR(:   ,N3-1)

RETURN
END
!**********************************************************************
subroutine sfcopqr_var(no,mof,np,niq,njq,n2,n3,xt,yt,platn,plonn  &
     ,erad,deltallo,deltaxp,deltayp,deltaxq,deltayq,iblksizo  &
     ,isbego,iwbego,dato,datp,datq,datr  &
     ,ofn,offlat,offlon,vnam,ngr,itopsflg,iz0flg,iii,jjj)
implicit none
integer :: no,mof,np,niq,njq,n2,n3,iblksizo,isbego,iwbego,ngr  &
          ,itopsflg,iz0flg,iii,jjj
real :: dato(no,no,mof),datp(np,np),datq(niq,njq),datr(n2,n3)  &
         ,xt(n2),yt(n3)
real :: erad,deltallo,deltaxp,deltayp,deltaxq,deltayq,offlat,offlon  
character(len=80) :: ofn,title3
character(len=3) :: title1,vnam
character(len=4) :: title2
logical l1,l2
integer,parameter :: maxmiss=1000
character(len=80) :: fnmiss(maxmiss)
real, allocatable :: sdq(:,:),shaq(:,:),sdr(:,:),datre(:,:)
real, allocatable :: iso(:),iwo(:)

real :: platn,plonn,xcentr,ycentr,glatp,glonp,rio_full,rjo_full  &
       ,xq,yq,xp,yp,wio1,wio2,wjo1,wjo2,sha,rha,rh2,sh,rh &
       ,xq1,yq1,xr,yr,rval,diff,difflcl
integer :: nmiss,nono,nofr,iof,iq,jq,ip,jp,iwoc,isoc,io1,io2,jo1,jo2 &
          ,lb,nn,isocpt,isocpo,iwocpo,iwocph,iwocpt,io_full,jo_full &
          ,iofr,jofr,ir,jr,is,js,i,j



allocate (sdq(niq,njq),shaq(niq,njq),sdr(n2,n3),datre(n2,n3))
allocate (iso(mof),iwo(mof))

nmiss=0

nono=no*no
!- para versao 1
!XCENTR=0.5*(XT(1)+XT(N2))   
!YCENTR=0.5*(YT(1)+YT(N3))   
!- para versao OPT
XCENTR=0.5*(XT(III)+XT(III+1)) 
YCENTR=0.5*(YT(JJJ)+YT(JJJ+1)) 


NOFR=0
DO IOF=1,MOF
   ISO(IOF)=0
   IWO(IOF)=0
ENDDO
DO JQ=1,NJQ
   DO IQ=1,NIQ
      XQ=(FLOAT(IQ)-0.5*FLOAT(NIQ+1))*DELTAXQ+XCENTR
      YQ=(FLOAT(JQ)-0.5*FLOAT(NJQ+1))*DELTAYQ+YCENTR
      DO JP=1,NP
         DO IP=1,NP
            XP=XQ+(FLOAT(IP)-0.5*FLOAT(NP+1))*DELTAXP
            YP=YQ+(FLOAT(JP)-0.5*FLOAT(NP+1))*DELTAYP

            call xy_ll(GLATP,GLONP,platn,plonn,xp,yp)

            glatp = max(-89.9999,min(89.9999,glatp - offlat))
!srf-rams60 mod
!            glonp = glonp - offlon
            glonp = max(-179.999,min(179.999,glonp - offlon))
!
            if (glonp >=  180.) glonp = glonp - 360.
            if (glonp <= -180.) glonp = glonp + 360.

            rio_full = (glonp - float(iwbego)) / deltallo
            rjo_full = (glatp - float(isbego)) / deltallo

            io_full = int(rio_full)
            jo_full = int(rjo_full)

            iwoc = (io_full / (no-1)) * iblksizo + iwbego
            isoc = (jo_full / (no-1)) * iblksizo + isbego

            wio2 = rio_full - float(io_full)
            wjo2 = rjo_full - float(jo_full)
           
            wio1 = 1. - wio2
            wjo1 = 1. - wjo2

            io1 = mod(io_full,no-1) + 1
            jo1 = mod(jo_full,no-1) + 1

            io2 = io1 + 1
            jo2 = jo1 + 1
            
            DO IOFR=1,NOFR
               JOFR=IOFR
               IF(ISO(IOFR).EQ.ISOC.AND.IWO(IOFR).EQ.IWOC)GO TO 10
            ENDDO

!                  not using dted data
            if(vnam.ne.'TOD'.and.vnam.ne.'ZOD') then
               ISOCPT=ABS(ISOC)/10
               ISOCPO=ABS(ISOC)-ISOCPT*10
               IWOCPH=ABS(IWOC)/100
               IWOCPT=(ABS(IWOC)-IWOCPH*100)/10
               IWOCPO=ABS(IWOC)-IWOCPH*100-IWOCPT*10
               IF(ISOC.GE.0) THEN
                  WRITE(TITLE1,'(2I1,A1)')ISOCPT,ISOCPO,'N'
               ELSE
                  WRITE(TITLE1,'(2I1,A1)')ISOCPT,ISOCPO,'S'
               ENDIF
               IF(IWOC.GE.0) THEN
                  WRITE(TITLE2,'(3I1,A1)')IWOCPH,IWOCPT,IWOCPO,'E'
               ELSE
                  WRITE(TITLE2,'(3I1,A1)')IWOCPH,IWOCPT,IWOCPO,'W'
               ENDIF
               LB=len_trim(OFN)
               TITLE3=OFN(1:LB)//TITLE1//TITLE2
               LB=len_trim(TITLE3)
               INQUIRE(FILE=TITLE3(1:LB),EXIST=L1,OPENED=L2)

               IF(.NOT.L1)THEN
                  do nn=1,nmiss
                     if(TITLE3(1:LB).eq.fnmiss(nn)) goto 302
                  enddo
                  nmiss=nmiss+1
                  fnmiss(nmiss)=TITLE3(1:LB)
302                    continue
                  DATP(IP,JP)=0.
                  GOTO 20
               ENDIF
            ENDIF

            IF(NOFR.GE.MOF) THEN
               DO IOF=1,MOF
                  ISO(IOF)=0
                  IWO(IOF)=0
               ENDDO
               NOFR=0
            ENDIF
            NOFR=NOFR+1
            JOFR=NOFR

!                 using dted data
            if(vnam.eq.'TOD'.or.vnam.eq.'ZOD') then
               call dted(no,ofn,isoc,iwoc,dato(1,1,nofr))
            else
               call rams_f_open  &
                    (29,TITLE3(1:LB),'FORMATTED','OLD','READ',0)
               CALL VFIREC(29,DATO(1,1,NOFR),NONO,'LIN')
               CLOSE(29)
            endif

            ISO(NOFR)=ISOC
            IWO(NOFR)=IWOC

10          CONTINUE

            datp(ip,jp)=wio1*(wjo1*dato(io1,jo1,jofr)   &
                             +wjo2*dato(io1,jo2,jofr))  &
                       +wio2*(wjo1*dato(io2,jo1,jofr)   &
                             +wjo2*dato(io2,jo2,jofr))
                                                          
20          CONTINUE
         ENDDO
      ENDDO

!           std dev for envelope orog and topo based zo schemes
      SHA=0.
      RHA=0.
      RH2=0.
      DO JP=1,NP
         SH=0.
         RH=0.
         DO IP=1,NP
            SH=MAX(SH,DATP(IP,JP))
            RH=RH+DATP(IP,JP)
            RH2=RH2+DATP(IP,JP)**2
         ENDDO
         SHA=SHA+SH/(2.*FLOAT(NP))
         RHA=RHA+RH
      ENDDO
      DATQ(IQ,JQ)=RHA/FLOAT(NP*NP)
      SDQ(IQ,JQ)=SQRT(max(0.,RH2/NP**2-DATQ(IQ,JQ)**2))
      DO IP=1,NP
         SH=0.
         DO JP=1,NP
            SH=MAX(SH,DATP(IP,JP))
         ENDDO
         SHA=SHA+SH/(2.*FLOAT(NP))
      ENDDO
      SHAQ(IQ,JQ)=SHA
      
   ENDDO
!         print*,'finished sfcopqr row jq = ',jq
ENDDO

!     envelope and zo schemes

if((vnam.eq.'TOP'.or.vnam.eq.'TOD').and.  &
   (ITOPSFLG.eq.2.or.ITOPSFLG.eq.3).and.  &
   NP*NP.lt.8) print*,'Warning - '  &
   ,'trying to calc a std dev for: ',NP*NP,' points'
if((vnam.eq.'ZOT'.or.vnam.eq.'ZOD').and.  &
   IZ0FLG.eq.1.and.NP*NP.lt.8) print*,'Warning - '  &
   ,'trying to calc a std dev for: ',NP*NP,' points'

if(vnam.eq.'TOP'.or.vnam.eq.'TOD')  &
   CALL TOPOQ(NIQ,NJQ,DELTAXQ,DELTAYQ,DATQ,SDQ,SHAQ,DATRE  &
           ,NGR,N2,N3)
if(vnam.eq.'ZOT'.or.vnam.eq.'ZOD')  &
   CALL ZOQ(NIQ,NJQ,DATQ,SDQ,NGR)

XQ1=(1.-0.5*FLOAT(NIQ+1))*DELTAXQ+XCENTR
YQ1=(1.-0.5*FLOAT(NJQ+1))*DELTAYQ+YCENTR


!-srf ::: telesc
!DO JR=1,N3
!   DO IR=1,N2
DO JR=jjj,jjj
   DO IR=iii,iii
      XR=(XT(IR)-XQ1)/DELTAXQ+1.
      YR=(YT(JR)-YQ1)/DELTAYQ+1.
      CALL GDTOST(DATQ,NIQ,NJQ,XR,YR,RVAL)

!           envelope orog and zo schemes

      if(vnam.eq.'ZOT'.or.vnam.eq.'ZOD') then
         DATR(IR,JR)=MAX(0.,RVAL)
!               print*,'z0r',IR,JR,DATR(IR,JR)
      else
         DATR(IR,JR)=MAX(0.,RVAL)
      endif
      
      CALL GDTOST(SDQ,NIQ,NJQ,XR,YR,RVAL)
      SDR(IR,JR)=MAX(0.,RVAL)
   ENDDO
ENDDO

if(nmiss.gt.0) then
   print*,'-----------------------------------------------------'
   print*,'Input physiographical data file processing:'
   print*,'-----------------------------------------------------'
   print*,'  Input data blocks not found '  &
        ,' (data assumed to be zero):'
   do nn=1,nmiss
      print*,fnmiss(nn)
   enddo
   print*,'-----------------------------------------------------'
endif


!srf ELIMINAR PARA telescopica
go to 222


!     check to find the largest change in topo height

if(vnam.eq.'TOP'.or.vnam.eq.'TOD') then
   diff=0.
   difflcl=0.
   is=-999
   js=-999
   do j=2,n3-1
      do i=2,n2-1
!               print*,'1=',difflcl,max(difflcl,abs(datr(i,j)-datr(i-1,j)))
         difflcl=max(difflcl,abs(datr(i,j)-datr(i-1,j)))
!               print*,'2=',difflcl,max(difflcl,abs(datr(i,j)-datr(i+1,j)))
         difflcl=max(difflcl,abs(datr(i,j)-datr(i+1,j)))
!               print*,'3=',difflcl,max(difflcl,abs(datr(i,j)-datr(i,j-1)))
         difflcl=max(difflcl,abs(datr(i,j)-datr(i,j-1)))
!               print*,'4=',difflcl,max(difflcl,abs(datr(i,j)-datr(i,j+1)))
         difflcl=max(difflcl,abs(datr(i,j)-datr(i,j+1)))
         if(abs(diff-difflcl).gt.1.) then
            is=i
            js=j
         endif
         diff=max(diff,difflcl)
      enddo
   enddo
   write(6,100) ' Max d(topo) on grid @i,j=',ngr,is,js,diff
100     format(a,3i4,f8.1)
endif


!srf
222 continue

deallocate(SDQ,SHAQ,SDR,DATRE)
deallocate(ISO,IWO)

RETURN
END

!**********************************************************************

subroutine get_TOPTWVL(ngr,n2,n3,TOPTWVL_2d)
use mem_grid
use io_params
implicit none
integer, intent(in) :: n2,n3,ngr
real, intent (out) :: TOPTWVL_2d(n2,n3)
real rlat(n2,n3),rlon(n2,n3)
integer i,j    

!print*,n2,n3,platn(ngr),plonn(ngr),xmn(1,ngr),ymn(1,ngr)
!-calculate lat, lon of each grid box T-points
do j=1,n3
 do i=1,n2
    call xy_ll (rlat(i,j),rlon(i,j),platn(ngr),plonn(ngr),xmn(i,ngr),ymn(j,ngr))
 enddo
enddo    
    
    
do j=1,n3
 do i=1,n2
    TOPTWVL_2d(i,j)=abs(TOPTWVL(NGR))
    
    if(rlat(i,j) .lt. -15.) then
     if(rlon(i,j).lt.-60.) TOPTWVL_2d(i,j)=15.
    endif
    
    if(rlat(i,j).ge.-15. .and. rlat(i,j).lt.-9.) then
      if(rlon(i,j).lt.-62.) TOPTWVL_2d(i,j)=15.
    endif

    if(rlat(i,j).ge.-9. .and. rlat(i,j).lt.-1.) then
      if(rlon(i,j).lt.-70.) TOPTWVL_2d(i,j)=15.
    endif 
    
    if(rlat(i,j).ge.-1.) then
     if(rlon(i,j).le.-57.and.rlon(i,j).gt.-67.) TOPTWVL_2d(i,j)=11.
    endif 
    
    if(rlat(i,j).ge.-1.) then
      if(rlon(i,j).le.-67.)  TOPTWVL_2d(i,j)=15.
    endif
    
    if(rlat(i,j).le.-10.) then
      if(rlon(i,j).ge.40.) TOPTWVL_2d(i,j)=11.
    endif
!print*,i,j,TOPTWVL_2d(i,j)
 enddo
enddo    
end subroutine get_TOPTWVL
!**********************************************************************
!-------------------------------------------------------------

subroutine get_akmin2d(ngr,n2,n3)
use mem_grid
use io_params
!tmp-srf para diferente difusao numerica >
use extras, only:   extra2d,NA_EXTRA2D

implicit none
integer, intent(in) :: n2,n3,ngr
real rlat(n2,n3),rlon(n2,n3)
integer i,j    
 !srf-define diferentes AKMINs para melhorar estabilidade 
 !srf-sobre os Andes
 !testa numero de extras:
 if(NA_EXTRA2D < 5) stop 'NA_EXTRA2d deve ser no minimo 5' 
 !default 
 extra2d(5,ngr)%d2(:,:)=1.
 !----

!print*,n2,n3,platn(ngr),plonn(ngr),xmn(1,ngr),ymn(1,ngr)
!-calculate lat, lon of each grid box T-points
do j=1,n3
 do i=1,n2
    call xy_ll (rlat(i,j),rlon(i,j),platn(ngr),plonn(ngr),xmn(i,ngr),ymn(j,ngr))
 enddo
enddo    
    
    
do j=1,n3
 do i=1,n2
    
    if(rlat(i,j) .lt. -15.) then
     if(rlon(i,j).lt.-60.) then 
!       TOPTWVL_2d(i,j)=15.
       extra2d(5,ngr)%d2(i,j)=3.
    endif;endif
    
    if(rlat(i,j).ge.-15. .and. rlat(i,j).lt.-9.) then
      if(rlon(i,j).lt.-62.) then
!       TOPTWVL_2d(i,j)=15.
       extra2d(5,ngr)%d2(i,j)=3.
    endif;endif

    if(rlat(i,j).ge.-9. .and. rlat(i,j).lt.-1.) then
      if(rlon(i,j).lt.-70.) then 
!      TOPTWVL_2d(i,j)=15.
       extra2d(5,ngr)%d2(i,j)=3.
    endif;endif
    
    if(rlat(i,j).ge.-1.) then
     if(rlon(i,j).le.-57.and.rlon(i,j).gt.-67.) then
!      TOPTWVL_2d(i,j)=11.
      extra2d(5,ngr)%d2(i,j)=1.5
    endif;endif
    
    if(rlat(i,j).ge.-1.) then
      if(rlon(i,j).le.-67.) then
!       TOPTWVL_2d(i,j)=15.
      extra2d(5,ngr)%d2(i,j)=3.
    endif;endif

    
    if(rlat(i,j).le.-10.) then
      if(rlon(i,j).ge.40.) then 
!      TOPTWVL_2d(i,j)=11.
      extra2d(5,ngr)%d2(i,j)=1.5
    endif;endif
 enddo
enddo    

end subroutine get_akmin2d
!**********************************************************************


